forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
form-largest-integer-with-digits-that-add-up-to-target.cpp
63 lines (61 loc) · 1.73 KB
/
form-largest-integer-with-digits-that-add-up-to-target.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
// Time: O(t)
// Space: O(t)
class Solution {
public:
string largestNumber(vector<int>& cost, int target) {
vector<int> dp(1);
dp[0] = 0;
for (int t = 1; t <= target; ++t) {
dp.emplace_back(-1);
for (int i = 0; i < 9; ++i) {
if (t < cost[i] || dp[t - cost[i]] < 0) {
continue;
}
dp[t] = max(dp[t], dp[t - cost[i]] + 1);
}
}
if (dp[target] < 0) {
return "0";
}
string result;
for (int i = 8; i >= 0; --i) {
while (target >= cost[i] && dp[target] == dp[target - cost[i]] + 1) {
target -= cost[i];
result.push_back('1' + i);
}
}
return result;
}
};
// Time: O(t)
// Space: O(t)
class Solution2 {
public:
string largestNumber(vector<int>& cost, int target) {
const auto& key = [](const auto& a) {
return pair{accumulate(cbegin(a), cend(a), 0), a};
};
vector<vector<int>> dp = {vector<int>(9)};
for (int t = 1; t <= target; ++t) {
dp.emplace_back();
for (int i = 0; i < 9; ++i) {
if (t < cost[i] || dp[t - cost[i]].empty()) {
continue;
}
auto curr = dp[t - cost[i]];
++curr[8 - i];
if (key(curr) > key(dp[t])) {
dp[t] = move(curr);
}
}
}
if (dp.back().empty()) {
return "0";
}
string result;
for (int i = 0; i < dp.back().size(); ++i) {
result += string(dp.back()[i], '1' + 8 - i);
}
return result;
}
};