Skip to content

Commit b1393b9

Browse files
authored
Update form-largest-integer-with-digits-that-add-up-to-target.cpp
1 parent f4cbd56 commit b1393b9

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

C++/form-largest-integer-with-digits-that-add-up-to-target.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
class Solution {
55
public:
66
string largestNumber(vector<int>& cost, int target) {
7-
vector<int> dp(target + 1, -1);
7+
vector<int> dp(1);
88
dp[0] = 0;
99
for (int t = 1; t <= target; ++t) {
10+
dp.emplace_back(-1);
1011
for (int i = 0; i < 9; ++i) {
1112
if (t < cost[i] || dp[t - cost[i]] < 0) {
1213
continue;
@@ -20,8 +21,8 @@ class Solution {
2021
string result;
2122
for (int i = 8; i >= 0; --i) {
2223
while (target >= cost[i] && dp[target] == dp[target - cost[i]] + 1) {
23-
result.push_back('1' + i);
2424
target -= cost[i];
25+
result.push_back('1' + i);
2526
}
2627
}
2728
return result;

0 commit comments

Comments
 (0)