Form Largest Integer With Digits That Add up to Target LeetCode solution in Python
1from functools import lru_cache234class Solution:5def largestNumber(self, cost: List[int], target: int) -> str:6@lru_cache(None)7def dfs(t):8if t == 0:9return 010res = float("-inf")11for digit in range(1, 10):12if t - cost[digit - 1] >= 0:13res = max(res, dfs(t - cost[digit - 1]) * 10 + digit)14return res1516res = dfs(target)17return "0" if res == float("-inf") else str(res)