aboutsummaryrefslogtreecommitdiff
path: root/other/largest_int_add_target.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/largest_int_add_target.cpp
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/largest_int_add_target.cpp')
-rw-r--r--other/largest_int_add_target.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/other/largest_int_add_target.cpp b/other/largest_int_add_target.cpp
new file mode 100644
index 0000000..91015d0
--- /dev/null
+++ b/other/largest_int_add_target.cpp
@@ -0,0 +1,29 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ string largestNumber(vector<int> &cost, int target) {
+ vector<int> dp(target + 1, -10000);
+ dp[0] = 0;
+ for (int i = 1; i <= target; ++i)
+ for (int j = 0; j < 9; ++j)
+ dp[i] = max(dp[i], i >= cost[j] ? 1 + dp[i - cost[j]] : -10000);
+ if (dp[target] < 0)
+ return "0";
+ string ans = "";
+ for (int i = 8; i >= 0; --i) {
+ while (target >= cost[i] && dp[target] == dp[target - cost[i]] + 1) {
+ ans.push_back('1' + i);
+ target -= cost[i];
+ }
+ }
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> cost = {4, 3, 2, 5, 6, 7, 2, 5, 5};
+ int target = 9;
+ cout << obj.largestNumber(cost, target);
+}