-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaintHouse.cpp
More file actions
27 lines (24 loc) · 838 Bytes
/
PaintHouse.cpp
File metadata and controls
27 lines (24 loc) · 838 Bytes
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
class Solution {
public:
/**
* @param costs: n x 3 cost matrix
* @return: An integer, the minimum cost to paint all houses
*/
int minCost(vector<vector<int>> &costs) {
// write your code here
if (costs.empty() || costs[0].empty()) return 0;
int m = costs.size();
int n = costs[0].size();
vector<vector<int>> dp(m, vector<int>(n, 0));
dp[0][0] = costs[0][0];
dp[0][1] = costs[0][1];
dp[0][2] = costs[0][2];
for (int i = 1; i < m; i++)
{
dp[i][0] = min(dp[i - 1][1], dp[i - 1][2]) + costs[i][0];
dp[i][1] = min(dp[i - 1][0], dp[i - 1][2]) + costs[i][1];
dp[i][2] = min(dp[i - 1][0], dp[i - 1][1]) + costs[i][2];
}
return min(min(dp[m - 1][0], dp[m - 1][1]), dp[m - 1][2]);
}
};