[JAVA] DP / T:O(N), S:O(1)


  • 0
    J
    class Solution {
        public int minCost(int[][] costs) {
            int[] pre_cost = new int[3];
            
            for(int[] cost : costs){
                int red = cost[0] + Math.min(pre_cost[1], pre_cost[2]);
                int green = cost[1] + Math.min(pre_cost[0], pre_cost[2]);
                int blue = cost[2] + Math.min(pre_cost[0], pre_cost[1]);
                
                pre_cost[0] = red;
                pre_cost[1] = green;
                pre_cost[2] = blue;
            }
            
            return Math.min(Math.min(pre_cost[0], pre_cost[1]), pre_cost[2]);
        }
    }
    

Log in to reply
 

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