Minimum Cost For Tickets LeetCode solution in Java

1
class Solution {
2
public int mincostTickets(int[] days, int[] costs) {
3
HashSet<Integer> set = new HashSet<>();
4
for (int day : days) set.add(day);
5
int n = days[days.length - 1];
6
int[] dp = new int[n + 1];
7
Arrays.fill(dp, Integer.MAX_VALUE);
8
dp[0] = 0;
9
for (int i = 1; i <= n; i++) {
10
if (!set.contains(i)) {
11
dp[i] = dp[i - 1];
12
continue;
13
}
14
int a = dp[Math.max(0, i - 1)] + costs[0];
15
int b = dp[Math.max(0, i - 7)] + costs[1];
16
int c = dp[Math.max(0, i - 30)] + costs[2];
17
dp[i] = Math.min(a, Math.min(b, c));
18
}
19
return dp[n];
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0