-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_515_PaintHouse.cpp
More file actions
42 lines (31 loc) · 1.06 KB
/
Copy path_515_PaintHouse.cpp
File metadata and controls
42 lines (31 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* Source - https://www.lintcode.com/problem/paint-house/description/
Author - Shivam Arora
*/
#include <bits/stdc++.h>
using namespace std;
int minCost(vector<vector<int>> &costs) {
if (costs.empty())
return 0;
int n = costs.size();
int minCost = 0, red = costs[0][0], blue = costs[0][1], green = costs[0][2];
for (int i = 1; i < n; i++) {
int j = 0, rtemp = red, btemp = blue, gtemp = green;
red = costs[i][j++] + min(btemp, gtemp);
blue = costs[i][j++] + min(rtemp, gtemp);
green = costs[i][j++] + min(rtemp, btemp);
}
return min(red, min(blue, green));
}
int main()
{
int n;
cout<<"Enter number of houses: ";
cin>>n;
vector<vector<int>> costs(n, vector<int> (3));
cout<<"Enter costs row-wise: "<<endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++)
cin>>costs[i][j];
}
cout<<"Minimum cost to paint the houses with 3 colors such that no two consecutive houses have same color: "<<minCost(costs)<<endl;
}