2431. Maximize Total Tastiness of Purchased Fruits

Updated: 2024-03-12
1 min read
[]

On This Page

LeetCode problem 2431

class Solution:
    def maxTastiness(
        self, price: List[int], tastiness: List[int], maxAmount: int, maxCoupons: int
    ) -> int:
        @cache
        def dfs(i, j, k):
            if i == len(price):
                return 0
            res = dfs(i + 1, j, k)
            if j >= price[i]:
                res = max(res, dfs(i + 1, j - price[i], k) + tastiness[i])
            if j >= price[i] // 2 and k:
                res = max(res, dfs(i + 1, j - price[i] // 2, k - 1) + tastiness[i])
            return res

        return dfs(0, maxAmount, maxCoupons)