-
Notifications
You must be signed in to change notification settings - Fork 0
/
minpaint.cpp
74 lines (63 loc) · 1.87 KB
/
minpaint.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
// minpaint.cpp
// xcode
//
// Created by Gokul Nadathur on 8/19/16.
// Copyright © 2016 Gokul Nadathur. All rights reserved.
//
#include <vector>
#include <utility>
using namespace std;
class Solution {
public:
typedef pair<int, int> Cumulative;
void initMinE(Cumulative& minE, Cumulative& nextMin, int c, int j)
{
if (c >= nextMin.first) {
return;
}
if (c <= minE.first) {
nextMin = minE;
minE.first = c;
minE.second = j;
} else {
nextMin.first = c;
nextMin.second = j;
}
}
int findExcl(vector<vector<Cumulative>>& v, int i, int excl)
{
return v[i][0].second != excl ? 0 : 1;
}
void updateMin(vector<vector<Cumulative>>& v, vector<vector<int>>& costs, int i)
{
Cumulative minE(INT_MAX, 0), nextMin(INT_MAX, 0);
if (i == 0) {
auto& cost = costs[i];
for (auto j = 0; j < cost.size(); ++j) {
auto c = cost[j];
initMinE(minE, nextMin, c, j);
}
} else {
auto& cost = costs[i];
for (auto j = 0; j < cost.size(); ++j) {
auto index = findExcl(v, i - 1, j);
auto c = v[i-1][index].first + cost[j];
initMinE(minE, nextMin, c, j);
}
}
//cout << "min " << minE.first << " next " << nextMin.first << endl;
v[i][0] = minE;
v[i][1] = nextMin;
}
int minCostII(vector<vector<int>>& costs) {
if (costs.size() == 0) {
return 0;
}
vector<vector<Cumulative>> v(costs.size(), vector<Cumulative>(2,Cumulative(0,0)));
for (auto i = 0; i < costs.size(); ++i) {
updateMin(v, costs, i);
}
return v[costs.size() - 1][0].first;
}
};