Paint House II in C++


Suppose we have n houses in a row, now each house can be painted with one of the k colors. The painting cost of each house with a certain color is different. Now we have to keep in mind that we have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by a matrix of order n x k. And we have to find the minimum cost to paint all houses.

So, if the input is like

153
294

then the output will be 5, as paint house 0 into color 0, paint house 1 into color 2. Then the minimum cost is 1 + 4 = 5; Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost is 3 + 2 = 5.

To solve this, we will follow these steps −

  • n := row size of costs

  • m := (if n is non-zero, then col size of costs, otherwise 0)

  • ret := inf

  • for initialize i := 1, when i <n, update (increase i by 1), do −

    • req := inf

    • Define an array lmins of size m

    • Define an array rmins of size m

    • lmins[0] := costs[i - 1, 0]

    • rmins[m - 1] = costs[i - 1, m - 1]

    • for initialize j := 1, when j < m, update (increase j by 1), do −

      • lmins[j] := minimum of costs[i - 1, j] and lmins[j - 1]

    • for initialize j := m - 2, when j >= 0, update (decrease j by 1), do −

      • rmins[j] := minimum of costs[i - 1, j] and rmins[j + 1]

    • for initialize j := 0, when j < m, update (increase j by 1), do −

      • left := (if j - 1 >= 0, then lmins[j - 1], otherwise inf)

      • right := (if j + 1 < m, then rmins[j + 1], otherwise inf)

      • costs[i, j] := costs[i, j] + min(left, right)

  • for initialize i := 0, when i < m, update (increase i by 1), do −

    • ret := minimum of ret and costs[n - 1, i]

  • return (if ret is same as inf, then 0, otherwise ret)

Example 

Let us see the following implementation to get better understanding −

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   int minCostII(vector<vector<int>>& costs) {
      int n = costs.size();
      int m = n ? costs[0].size() : 0;
      int ret = INT_MAX;
      for (int i = 1; i < n; i++) {
         int req = INT_MAX;
         vector<int> lmins(m);
         vector<int> rmins(m);
         lmins[0] = costs[i - 1][0];
         rmins[m - 1] = costs[i - 1][m - 1];
         for (int j = 1; j < m; j++) {
            lmins[j] = min(costs[i - 1][j], lmins[j - 1]);
         }
         for (int j = m - 2; j >= 0; j--) {
            rmins[j] = min(costs[i - 1][j], rmins[j + 1]);
         }
         for (int j = 0; j < m; j++) {
            int left = j - 1 >= 0 ? lmins[j - 1] : INT_MAX;
            int right = j + 1 < m ? rmins[j + 1] : INT_MAX;
            costs[i][j] += min(left, right);
         }
      }
      for (int i = 0; i < m; i++) {
         ret = min(ret, costs[n - 1][i]);
      }
      return ret == INT_MAX ? 0 : ret;
   }
};
main(){
   Solution ob;
   vector<vector<int>> v = {{1,5,3},{2,9,4}};
   cout <<(ob.minCostII(v));
}

Input

{{1,5,3},{2,9,4}}

Output

5

Updated on: 21-Jul-2020

295 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements