-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.java
40 lines (32 loc) · 996 Bytes
/
main.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
class Solution {
public int mincostTickets(int[] days, int[] costs) {
int n = 365;
// vector<int> f(n + 1);
int[] f = new int[n + 1];
int k = days.length;
int last = days[k - 1];
// set<int> s;
Set<Integer> s = new HashSet<Integer>();
for (int x : days)
s.add(x);
f[0] = 0;
for (int i = 1; i <= n; i++) {
if (!s.contains(i)) {
f[i] = f[i - 1];
continue;
}
f[i] = f[i - 1] + costs[0];
if (i - 7 >= 0)
f[i] = Math.min(f[i], f[i - 7] + costs[1]);
else
f[i] = Math.min(f[i], costs[1]);
if (i - 30 >= 0)
f[i] = Math.min(f[i], f[i - 30] + costs[2]);
else
f[i] = Math.min(f[i], costs[2]);
if (i == last)
break;
}
return f[last];
}
}