Skip to content

Commit

Permalink
1449
Browse files Browse the repository at this point in the history
  • Loading branch information
luanshiyinyang committed Nov 28, 2021
1 parent 223d184 commit 0ff0477
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions 1449-Form Largest Integer With Digits That Add up to Target.py
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)

0 comments on commit 0ff0477

Please sign in to comment.