Clean 9 lines of code, 2ms O(n) time and O(1) space


  • 0
    Q

    The idea is to buffering paint red, blue and green minimal costs of i-1 houses and make decision

    public class Solution {
        public int minCost(int[][] costs) {
            if(costs.length == 0){
                return 0;
            }
            for(int i = 1; i < costs.length; ++i){
                costs[i][0] = Math.min(costs[i][0] + costs[i-1][1], costs[i][0] + costs[i-1][2]);
                costs[i][1] = Math.min(costs[i][1] + costs[i-1][0], costs[i][1] + costs[i-1][2]);
                costs[i][2] = Math.min(costs[i][2] + costs[i-1][0], costs[i][2] + costs[i-1][1]);
            }
            return Math.min(Math.min(costs[costs.length-1][0],costs[costs.length-1][1]),costs[costs.length-1][2]);
        }
    }

Log in to reply
 

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