Skip to content

Commit 716582b

Browse files
Sean PrashadSean Prashad
authored andcommitted
Add 983_Minimum_Cost_For_Tickets.java
1 parent 5212b90 commit 716582b

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int mincostTickets(int[] days, int[] costs) {
3+
int n = days[days.length - 1];
4+
5+
boolean[] travelDay = new boolean[n + 1];
6+
for (int day : days) {
7+
travelDay[day] = true;
8+
}
9+
10+
int[] dp = new int[n + 1];
11+
dp[0] = 0;
12+
13+
for (int i = 1; i <= n; i++) {
14+
if (!travelDay[i]) {
15+
dp[i] = dp[i - 1];
16+
continue;
17+
}
18+
19+
int minCost = dp[i - 1] + costs[0];
20+
minCost = Math.min(minCost, dp[Math.max(0, i - 7)] + costs[1]);
21+
minCost = Math.min(minCost, dp[Math.max(0, i - 30)] + costs[2]);
22+
dp[i] = minCost;
23+
}
24+
25+
return dp[n];
26+
}
27+
}

0 commit comments

Comments
 (0)