Created
July 9, 2019 18:12
-
-
Save sogwiz/b27708524bd041b57e3eae5f3482cd5e 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
package com.learning.leet.problems; | |
import java.util.HashSet; | |
import java.util.Set; | |
/** | |
* Created by sargonbenjamin on 7/9/19. | |
* https://leetcode.com/problems/minimum-cost-for-tickets/ | |
*/ | |
public class MinCostForTickets { | |
public int mincostTickets(int[] days, int[] costs) { | |
int[] minCost = new int[366]; | |
Set<Integer> setRideDays = new HashSet<>(); | |
for(int day : days){ | |
setRideDays.add(day); | |
} | |
for(int i = 1; i<minCost.length; i++){ | |
if(setRideDays.contains(i)){ | |
int dayMinCost = minCost[Math.max(0,i-1)]+costs[0]; | |
int weekMinCost = minCost[Math.max(0,i-7)]+costs[1]; | |
int min = Math.min(dayMinCost,weekMinCost); | |
int monthCost = minCost[Math.max(0,i-30)]+costs[2]; | |
minCost[i]=Math.min(min,monthCost); | |
}else { | |
minCost[i]=minCost[i-1]; | |
} | |
} | |
return minCost[365]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment