Tap here to switch tabs
Problem
Submissions

Problem: Minimum Number of Coins for Fruits II

Minimum Number of Coins for Fruits II lists prices in order. Buying a fruit makes the next number of fruits equal to its position free. Return the minimum coins to collect all fruits.

Statement

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

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

When acquiring fruits, you can follow these rules:

  • If you purchase the ithi^{ {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

Minimum Number of Coins for Fruits II lists prices in order. Buying a fruit makes the next number of fruits equal to its position free. Return the minimum coins to collect all fruits.

Statement

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

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

When acquiring fruits, you can follow these rules:

  • If you purchase the ithi^{ {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