Skip to content

Commit

Permalink
Add Paint House III.cpp file
Browse files Browse the repository at this point in the history
This program solves issue Rohit0301#48
Question Link: https://leetcode.com/problems/paint-house-iii/
  • Loading branch information
atinder11 authored Oct 4, 2022
1 parent 19b9531 commit 5cd8486
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions Leetcode/Paint_House_III.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Question Link: https://leetcode.com/problems/paint-house-iii/
*/


class Solution {
public:

int dp[103][103][103];

int help(vector<int> &houses, vector<vector<int>> &cost, int m, int n, int target, int i, int prev) {

if (i == m) {
if (!target) return 0;
else return 1e9;
}

if (target < 0) return 1e9;

if (dp[i][prev][target] != -1) return dp[i][prev][target];


int mn = 1e9;

// case 1:
// no need to colorize
if (houses[i]) {
if (houses[i] != prev) // nbrs increase, hence target decreases
return dp[i][prev][target] = help(houses, cost, m, n, target - 1, i + 1, houses[i]);

else return dp[i][prev][target] = help(houses, cost, m, n, target, i + 1, houses[i]);
}

else {

// case 2: color them

// I need to paint with colors [1,n] such that the current color isn't the same as the prev
// if they are the same, nbrs remain the same (no change in target)
// else, nbrs increase, so target-1

// colors: 1 to n
// cost[i][j]: cost to paint ith building with color j+1


for (int c = 1; c <= n; c++) {
int cur = cost[i][c - 1];

if (prev == c) cur += help(houses, cost, m , n, target, i + 1, c);
else {

// explore the rest (next rows)
cur += help(houses, cost, m , n, target - 1, i + 1, c);
}
mn = min(mn, cur);
}

return dp[i][prev][target] = mn;
}


}

int minCost(vector<int>& houses, vector<vector<int>>& cost, int m, int n, int target) {

memset(dp, -1, sizeof(dp));
int mm = help(houses, cost, m, n, target, 0, 0);
return (mm >= 1e9) ? -1 : mm;
}
};

0 comments on commit 5cd8486

Please sign in to comment.