Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int minCostII(int[][] costs) {
- // write your code here
- if(costs.length==0) return 0;
- else{
- int[][] dp = new int[costs.length][costs[0].length];
- for(int i=0;i<dp[0].length;i++)
- dp[0][i] = costs[0][i];
- for(int i=1;i<dp.length;i++){
- for(int j=0;j<dp[0].length;j++){
- dp[i][j] = costs[i][j]+findMinCost(dp[i-1],j);
- }
- }
- int ans = dp[dp.length-1][0];
- for(int i=1;i<dp[0].length;i++){
- if(dp[dp.length-1][i]<ans)
- ans = dp[dp.length-1][i];
- }
- return ans;
- }
- }
- private int findMinCost(int[] row,int choice){
- int min = Integer.MAX_VALUE;
- for(int i=0;i<row.length;i++){
- if(i!=choice){
- if(row[i]<min)
- min = row[i];
- }
- }
- return min;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement