Created
August 26, 2020 04:07
-
-
Save IngeFrodo/8f11c31f1c6fa0b062bb461987e55ca8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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