-
Notifications
You must be signed in to change notification settings - Fork 46
/
_638_1.java
61 lines (52 loc) · 1.84 KB
/
_638_1.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.util.Arrays;
import java.util.List;
/**
* LeetCode 638 - Shopping Offers
* <p>
* Searching + pruning
*/
public class _638_1 {
public int shoppingOffers(List<Integer> price, List<List<Integer>> special, List<Integer> needs) {
class Solver {
int ans = Integer.MAX_VALUE;
public Solver() {
dfs(0, 0);
}
private void dfs(int t, int val) {
if (val >= ans) return;
int res = val;
for (int i = 0; i < needs.size(); i++)
res += needs.get(i) * price.get(i);
ans = Math.min(ans, res);
if (t >= special.size()) return;
int maxCnt = Integer.MAX_VALUE;
List<Integer> arr = special.get(t);
for (int i = 0; i < needs.size(); i++)
if (arr.get(i) > 0)
maxCnt = Math.min(maxCnt, needs.get(i) / arr.get(i));
dfs(t + 1, val);
for (int j = 0; j < maxCnt; j++) {
for (int i = 0; i < needs.size(); i++)
needs.set(i, needs.get(i) - arr.get(i));
val += arr.get(arr.size() - 1);
dfs(t + 1, val);
}
for (int i = 0; i < needs.size(); i++)
needs.set(i, needs.get(i) + maxCnt * arr.get(i));
}
}
Solver solver = new Solver();
return solver.ans;
}
public static void main(String[] args) {
_638_1 sol = new _638_1();
System.out.println(sol.shoppingOffers(
Arrays.asList(2, 5),
Arrays.asList(
Arrays.asList(3, 0, 5),
Arrays.asList(1, 2, 10)
),
Arrays.asList(3, 2)
));
}
}