Skip to content

Commit 23dabe3

Browse files
authored
feat: added leetcode 983 solution (#111)
1 parent bbaf8ed commit 23dabe3

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
int[] memo = new int[400];
3+
4+
public int solve(int i, int[] days, int[] costs) {
5+
if (i >= days.length) {
6+
return 0;
7+
}
8+
9+
if (memo[i] > 0) {
10+
return memo[i];
11+
}
12+
13+
memo[i] = 1000000;
14+
for (int j = 0; j < 3; j++) {
15+
int next = days[i] + ((j == 0) ? 1 : ((j == 1) ? 7: 30));
16+
int k = i;
17+
18+
while (k < days.length && days[k] < next) {
19+
k++;
20+
}
21+
22+
memo[i] = Math.min(memo[i], costs[j] + solve(k, days, costs));
23+
}
24+
25+
return memo[i];
26+
}
27+
28+
public int mincostTickets(int[] days, int[] costs) {
29+
return solve(0, days, costs);
30+
}
31+
}

0 commit comments

Comments
 (0)