Very easy to understand ACCEPTED recursive JAVA code! Don't miss it!

  • 0
    //Remember this soultion was posted to get some idea. Its not the most optimized solution or not even close to that.
    public class Solution {
        public int minCostII(int[][] costs) {
            return minCostHelper(costs,0,-1);
        Map<String,Integer> memoizeMap = new HashMap<>();
      // HouseId represent which house is getting painted
        public int minCostHelper(int[][] costs, int houseId, int ignoreColorId){
            if(houseId > costs.length -1 ){
                return 0;
                return memoizeMap.get(houseId+"-"+ignoreColorId);
            int minCost = Integer.MAX_VALUE;
            for(int color=0; color < costs[0].length; color++){
                if(color != ignoreColorId)   {
                    int currCost = costs[houseId][color];
                    int restOfTheCost = minCostHelper(costs, houseId+1, color);
                    minCost = Math.min(minCost,currCost+restOfTheCost);
            memoizeMap.put(houseId+"-"+ignoreColorId, minCost);
            return minCost;

Log in to reply

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