Skip to content

Commit 385d25f

Browse files
refactor 638
1 parent 4a14e9f commit 385d25f

File tree

1 file changed

+30
-26
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-26
lines changed

src/main/java/com/fishercoder/solutions/_638.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,36 +41,40 @@
4141
You are not allowed to buy more items than you want, even if that would lower the overall price.
4242
*/
4343
public class _638 {
44-
/**reference: https://leetcode.com/articles/shopping-offers/#approach-1-using-recursion-accepted*/
45-
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
46-
return shopping(price, special, needs, 0);
47-
}
48-
49-
public int shopping(List<Integer> price, List<List<Integer>> special, List<Integer> needs, int i) {
50-
if (i == special.size()) {
51-
return dot(needs, price);
44+
public static class Solution1 {
45+
/**
46+
* reference: https://leetcode.com/articles/shopping-offers/#approach-1-using-recursion-accepted
47+
*/
48+
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
49+
return shopping(price, special, needs, 0);
5250
}
53-
ArrayList<Integer> clone = new ArrayList(needs);
54-
int j = 0;
55-
for (j = 0; j < special.get(i).size() - 1; j++) {
56-
int diff = clone.get(j) - special.get(i).get(j);
57-
if (diff < 0) {
58-
break;
51+
52+
public int shopping(List<Integer> price, List<List<Integer>> special, List<Integer> needs, int i) {
53+
if (i == special.size()) {
54+
return dot(needs, price);
55+
}
56+
ArrayList<Integer> clone = new ArrayList(needs);
57+
int j = 0;
58+
for (j = 0; j < special.get(i).size() - 1; j++) {
59+
int diff = clone.get(j) - special.get(i).get(j);
60+
if (diff < 0) {
61+
break;
62+
}
63+
clone.set(j, diff);
64+
}
65+
if (j == special.get(i).size() - 1) {
66+
return Math.min(special.get(i).get(j) + shopping(price, special, clone, i), shopping(price, special, needs, i + 1));
67+
} else {
68+
return shopping(price, special, needs, i + 1);
5969
}
60-
clone.set(j, diff);
61-
}
62-
if (j == special.get(i).size() - 1) {
63-
return Math.min(special.get(i).get(j) + shopping(price, special, clone, i), shopping(price, special, needs, i + 1));
64-
} else {
65-
return shopping(price, special, needs, i + 1);
6670
}
67-
}
6871

69-
public int dot(List<Integer> a, List<Integer> b) {
70-
int sum = 0;
71-
for (int i = 0; i < a.size(); i++) {
72-
sum += a.get(i) * b.get(i);
72+
public int dot(List<Integer> a, List<Integer> b) {
73+
int sum = 0;
74+
for (int i = 0; i < a.size(); i++) {
75+
sum += a.get(i) * b.get(i);
76+
}
77+
return sum;
7378
}
74-
return sum;
7579
}
7680
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy