|
41 | 41 | You are not allowed to buy more items than you want, even if that would lower the overall price.
|
42 | 42 | */
|
43 | 43 | 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); |
52 | 50 | }
|
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); |
59 | 69 | }
|
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); |
66 | 70 | }
|
67 |
| - } |
68 | 71 |
|
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; |
73 | 78 | }
|
74 |
| - return sum; |
75 | 79 | }
|
76 | 80 | }
|
0 commit comments