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 buy fruit 0 (price 2, taste 3) and fruit 2 (price 1, taste 1). We can't afford fruit 1 at full price. Total cost: 2+1=3 ≤ 5, total taste: 3+1=4. Actually, we should buy fruits 0 and 1: cost 2+4=6 > 5, so we buy fruit 0 (cost 2, taste 3) and fruit 2 (cost 1, taste 1). Wait, let me recalculate: fruit 1 alone gives taste 6 for cost 4, plus fruit 2 gives cost 5 total and taste 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
Smart Fruit Shopping StrategyFruit StorePrice: $5Taste: ⭐⭐⭐⭐⭐Budget$10Coupons🎫 x2GoalMax TastinessWithin BudgetOption 1: SkipCost: $0Taste: 0Coupons: SameOption 2: FullCost: $5Taste: ⭐⭐⭐⭐⭐Coupons: SameOption 3: CouponCost: $2Taste: ⭐⭐⭐⭐⭐Coupons: -1DP Choice: Max TastinessConsider all combinationsChoose optimal strategy
Understanding the Visualization
1
Evaluate Each Fruit
For each fruit, calculate value at full price vs with coupon
2
Make Optimal Choice
Use DP to decide: skip, buy full price, or use precious coupon
3
Track Resources
Monitor remaining budget and available coupons
4
Maximize Tastiness
Choose combination that gives highest total tastiness
Key Takeaway
🎯 Key Insight: This is a 3D optimization problem where we must balance three constraints: fruit selection, budget management, and strategic coupon usage. Dynamic programming allows us to explore all possibilities efficiently while avoiding redundant calculations.
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