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