-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0956-tallest-billboard.cpp
64 lines (62 loc) · 2.04 KB
/
0956-tallest-billboard.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
// class Solution {
// public:
// int tallestBillboard(vector<int>& rods, int res = 0) {
// auto gen_key = [](int a, int b, int c) {
// return to_string(a) + "-" + to_string(b) + to_string(c);
// };
// unordered_set<string> c;
// function<void(int, int, int)> go = [&](auto i, auto l, auto r) {
// if (l == r) res = max(l, res);
// if (i == rods.size()) return;
// auto key = gen_key(i, l, r);
// if (c.count(key)) return;
// c.insert(key);
// go(i + 1, l + rods[i], r);
// go(i + 1, l, r + rods[i]);
// go(i + 1, l, r);
// };
// go(0, 0, 0);
// return res;
// }
// int tallestBillboard(vector<int>& rods) {
// unordered_map<int, int> dp;
// dp[0] = 0;
// for (int x : rods) {
// unordered_map<int, int> cur(dp);
// for (auto it: cur) {
// int d = it.first;
// dp[d + x] = max(dp[d + x],cur[d]);
// dp[abs(d - x)] = max(dp[abs(d - x)], cur[d] + min(d, x));
// }
// }
// return dp[0];
// }
// };
class Solution {
public:
// int tallestBillboard(vector<int>& rods) {
// int res = 0, n = rods.size();
// function<void(int, int, int)> go = [&](auto i, auto l, auto r) {
// if (l == r) res = max(res, l);
// if (i == n) return;
// go(i + 1, l + rods[i], r);
// go(i + 1, l, r + rods[i]);
// go(i + 1, l, r);
// };
// go(0, 0, 0);
// return res;
// }
int tallestBillboard(vector<int>& rods) {
unordered_map<int, int> dp;
dp[0] = 0;
for (int x : rods) {
unordered_map<int, int> cur(dp);
for (auto it: cur) {
int d = it.first;
dp[d + x] = max(dp[d + x], cur[d]);
dp[abs(d - x)] = max(dp[abs(d - x)], cur[d] + min(d, x));
}
}
return dp[0];
}
};