Fast Java solution


  • 0
    Z

    public class Solution {
    public int minCost(int[][] costs) {

        int n = costs.length, i;
        
        if(n == 0)
            return 0;
        
        int dp1 = costs[0][0], dp2 = costs[0][1],dp3 = costs[0][2];
        int temp1, temp2;
        
        for(i=1;i<n;i++) {
            temp1 = dp1;
            temp2 = dp2;
            dp1 = costs[i][0] + Math.min(dp2, dp3);
            dp2 = costs[i][1] + Math.min(temp1, dp3);
            dp3 = costs[i][2] + Math.min(temp1, temp2);
        }
        
        int ans = Math.min(Math.min(dp1 , dp2) , dp3);
        
        return ans;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.