-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
223d184
commit 0ff0477
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
1449-Form Largest Integer With Digits That Add up to Target.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
def largestNumber(self, cost: List[int], target: int) -> str: | ||
dp = [float("-inf")] * (target + 1) | ||
dp[0] = 0 | ||
for c in cost: | ||
for i in range(c, target+1): | ||
dp[i] = max(dp[i], dp[i-c] + 1) | ||
|
||
if dp[target] < 0: | ||
return "0" | ||
|
||
ans = [] | ||
j = target | ||
for i in range(8, -1, -1): | ||
c = cost[i] | ||
while j >= c and dp[j] == dp[j-c] + 1: | ||
j -= c | ||
ans.append(str(i+1)) | ||
|
||
return "".join(ans) |