Java DP solution


  • 0
    public class Solution {
        public int minCost(int[][] costs) {
            int houses = costs.length;
            int[][] res = new int[houses+1][3];
            for(int i = 1; i <= houses; i++){
                for(int j = 0; j < 3; j++){
                    res[i][j] = costs[i-1][j] + Math.min(res[i-1][(j+2)%3], res[i-1][(j+1)%3]);
                }
            }
            return Math.min(res[houses][0], Math.min(res[houses][1], res[houses][2]));
        }
    }
    

Log in to reply
 

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