Ctrl K

Market Fruit Bonus

Medium
Fruits lie in a row at a market; prices[i] is the price of fruit number i + 1 (numbered from one). The seller runs a promotion: buy fruit number i and you may take any of the next i fruits (numbers i + 1 through 2i) for free. A fruit you could take for free may still be bought, to get its own bonus. Return the least amount you need to pay to take home every fruit.
Example 1
prices=[4, 2, 9, 9] → 6

Buy fruit 1 for 4 and fruit 2 comes free; buy fruit 2 anyway for 2 and fruits 3 and 4 come free. Total 6.

Example 2
prices=[2, 7, 1, 8, 3] → 3

Buy fruit 1 (2), take fruit 2 free, buy fruit 3 (1), which covers fruits 4 and 5. Total 3.

Example 3
prices=[5] → 5
Constraints
  • 1 ≤ len(prices) ≤ 1000
  • 1 ≤ prices[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBest Score With Short Jumps MediumMaximum Sum With Short Hops HardFerry Runs From the Depot HardStops With Free Rides HardBest Run on a Ring Medium
loading Python…
Input
prices=[4, 2, 9, 9]
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 116 tests on the server, hidden ones included.