-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPaint_House_II.cpp
More file actions
29 lines (27 loc) · 852 Bytes
/
Paint_House_II.cpp
File metadata and controls
29 lines (27 loc) · 852 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
28
29
class Solution {
public:
int minCostII(vector<vector<int>>& costs) {
if(costs.empty()) return 0;
int n = (int)costs.size();
int m = (int)costs[0].size();
vector<vector<int>> dp(n, vector<int>(m, INT_MAX));
for(int i = 0; i < m; ++i) {
dp[0][i] = costs[0][i];
}
for(int i = 1; i < n; ++i) {
for(int j = 0; j < m; ++j) {
int cost = INT_MAX;
for(int k = 0; k < m; ++k) {
if(k == j) { continue; }
cost = min(cost, dp[i - 1][k]);
}
dp[i][j] = costs[i][j] + cost;
}
}
int result = INT_MAX;
for(int i = 0; i < m; ++i) {
result = min(result, dp[n - 1][i]);
}
return result;
}
};