Maximize Total Tastiness of Purchased Fruits - Problem

Imagine you're at a premium fruit market where each fruit has a specific price and tastiness rating. You have a limited budget (maxAmount) and want to maximize the total tastiness of fruits you purchase.

Here's the twist: you have discount coupons (maxCoupons) that let you buy certain fruits at half price (rounded down)! Your goal is to strategically use these coupons to get the maximum possible tastiness within your budget.

Input:

  • price[] - Price of each fruit
  • tastiness[] - Tastiness rating of each fruit
  • maxAmount - Your total budget
  • maxCoupons - Number of discount coupons available

Output: Maximum total tastiness you can achieve

Rules:

  • Each fruit can be purchased at most once
  • Each coupon can be used at most once
  • Coupons reduce price to price[i] // 2

Input & Output

example_1.py — Basic Example
$ Input: price = [3, 1, 2], tastiness = [4, 2, 3], maxAmount = 6, maxCoupons = 2
Output: 9
💡 Note: We can buy all three fruits using coupons on the expensive ones: fruit 0 with coupon (price 1, taste 4), fruit 1 at full price (price 1, taste 2), and fruit 2 with coupon (price 1, taste 3). Total cost: 1+1+1=3 ≤ 6, total taste: 4+2+3=9
example_2.py — Limited Budget
$ Input: price = [5, 3, 1, 6], tastiness = [8, 4, 2, 5], maxAmount = 4, maxCoupons = 1
Output: 6
💡 Note: Best strategy: buy fruit 2 at full price (cost 1, taste 2) and fruit 1 with coupon (cost 1, taste 4). Total cost: 1+1=2 ≤ 4, total taste: 2+4=6. We can't afford fruit 0 or 3 even with coupons.
example_3.py — No Coupons
$ Input: price = [2, 4, 1], tastiness = [3, 6, 1], maxAmount = 5, maxCoupons = 0
Output: 7
💡 Note: Without coupons, we can buy fruit 1 (price 4, taste 6) and fruit 2 (price 1, taste 1). Total cost: 4+1=5 ≤ 5, total taste: 6+1=7.

Constraints

  • 1 ≤ price.length, tastiness.length ≤ 1000
  • price.length == tastiness.length
  • 0 ≤ price[i], tastiness[i] ≤ 104
  • 0 ≤ maxAmount ≤ 104
  • 0 ≤ maxCoupons ≤ 1000
  • Each fruit can be purchased at most once
  • Each coupon can be used at most once

Visualization

Tap to expand
Maximize Total Tastiness of Purchased Fruits INPUT Fruit 0 price: 3 taste: 4 coupon:1 Fruit 1 price: 1 taste: 2 Fruit 2 price: 2 taste: 3 price = [3, 1, 2] tastiness = [4, 2, 3] budget = 6 coupons = 2 COUPON COUPON ALGORITHM (DP) 1 Define DP State dp[i][j][k] = max tastiness i=fruit, j=budget, k=coupons 2 For each fruit, choose: - Skip it - Buy full price - Buy with coupon (price//2) 3 Transition Take max of all choices 4 Return Result dp[n][maxAmount][maxCoupons] DP Table Sample 0 4 7 9 FINAL RESULT Optimal Selection Fruit 0 $3-->$1 taste:4 COUPON Fruit 1 $1 taste:2 Fruit 2 $2-->$1 taste:3 COUPON Cost Calculation Fruit 0: 3//2 = 1 (coupon) Fruit 1: 1 (full price) Fruit 2: 2//2 = 1 (coupon) Total: 1+1+1 = 3 <= 6 OK Maximum Tastiness 9 4 + 2 + 3 = 9 (all fruits!) Key Insight: This is a variant of 0/1 Knapsack with an additional dimension for coupons. For each fruit, we have 3 choices: skip it, buy at full price, or buy with coupon (if available). The DP tracks max tastiness for each combination of (fruits considered, remaining budget, remaining coupons). Time: O(n * maxAmount * maxCoupons) TutorialsPoint - Maximize Total Tastiness of Purchased Fruits | Dynamic Programming Approach
Asked in
Google 23 Amazon 31 Meta 18 Microsoft 15
23.1K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen