forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
28 lines (27 loc) · 919 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
int ans = total(price, needs);
List<Integer> t = new ArrayList<>();
for (List<Integer> offer : special) {
t.clear();
for (int j = 0; j < needs.size(); ++j) {
if (offer.get(j) > needs.get(j)) {
t.clear();
break;
}
t.add(needs.get(j) - offer.get(j));
}
if (!t.isEmpty()) {
ans = Math.min(ans, offer.get(offer.size() - 1) + shoppingOffers(price, special, t));
}
}
return ans;
}
private int total(List<Integer> price, List<Integer> needs) {
int s = 0;
for (int i = 0; i < price.size(); ++i) {
s += price.get(i) * needs.get(i);
}
return s;
}
}