Skip to content

Instantly share code, notes, and snippets.

@IngeFrodo
Created August 26, 2020 04:07
Show Gist options
  • Save IngeFrodo/8f11c31f1c6fa0b062bb461987e55ca8 to your computer and use it in GitHub Desktop.
Save IngeFrodo/8f11c31f1c6fa0b062bb461987e55ca8 to your computer and use it in GitHub Desktop.
class MinimumCostForTickets {
public int mincostTickets(int[] days, int[] costs) {
int maxDays = days[days.length - 1];
int[] dp = new int[maxDays + 1];
int index = 0;
for (int d = 1; d <= maxDays; d++) {
dp[d] = dp[d - 1];
if (d == days[index]) {
int oneDayPass = dp[d - 1] + costs[0];
int sevenDayPass = dp[Math.max(0, d - 7)] + costs[1];
int thirtyDayPass = dp[Math.max(0, d - 30)] + costs[2];
dp[d] = Math.min(oneDayPass, Math.min(sevenDayPass, thirtyDayPass));
index++;
}
}
return dp[maxDays];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment