Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Coins for Fruits II

med
30 min
Try to solve the Minimum Number of Coins for Fruits II problem.

Statement

You are given an integer array prices where prices[i] represents the price of the (i+1)extth(i + 1)^{ ext{th}} fruit.

Fruits are presented in order from the 1extst1^{ ext{st}} fruit to the nextthn^{ ext{th}} fruit, where nn is the length of prices.

When acquiring fruits, you can follow these rules:

  • If you purchase the iextthi^{ ext{th}} fruit (where 1≤i≤n1 \leq i \leq n) for prices[i - 1] coins, you are eligible to claim the next ii fruits for free, which includes all fruits from index i+1i + 1 up to index i+ii + i.

  • You may still decide to purchase a fruit even if you are allowed to claim it for free.

Return the minimum total number of coins required to collect all the fruits.

Constraints:

  • 1≤1 \leq prices.length ≤105\leq 10^5

  • 1≤1 \leq prices[i] ≤105\leq 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Coins for Fruits II

med
30 min
Try to solve the Minimum Number of Coins for Fruits II problem.

Statement

You are given an integer array prices where prices[i] represents the price of the (i+1)extth(i + 1)^{ ext{th}} fruit.

Fruits are presented in order from the 1extst1^{ ext{st}} fruit to the nextthn^{ ext{th}} fruit, where nn is the length of prices.

When acquiring fruits, you can follow these rules:

  • If you purchase the iextthi^{ ext{th}} fruit (where 1≤i≤n1 \leq i \leq n) for prices[i - 1] coins, you are eligible to claim the next ii fruits for free, which includes all fruits from index i+1i + 1 up to index i+ii + i.

  • You may still decide to purchase a fruit even if you are allowed to claim it for free.

Return the minimum total number of coins required to collect all the fruits.

Constraints:

  • 1≤1 \leq prices.length ≤105\leq 10^5

  • 1≤1 \leq prices[i] ≤105\leq 10^5