Tap here to switch tabs
Problem
Submissions

Problem: Maximum Fruits Harvested After at Most K Steps

med
30 min
Try to solve the Maximum Fruits Harvested After at Most K Steps problem.

Statement

Fruits are placed on an infinite number line. You are given a 22D integer array fruits where fruits[i] = [ position_i, amount_i ] indicates that there are amount_i fruits positioned at coordinate position_i. The array fruits is sorted by position_i in strictly ascending order.

You are also given two integers startPos and k. Initially, you are at position startPos. In one step, you can move 11 unit to the left or 11 unit to the right. You can walk at most k steps in total. Whenever you reach a coordinate containing fruits, you collect all fruits at that location. Each fruit can only be collected once.

Return the maximum total number of fruits you can collect.

Constraints:

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

  • fruits[i].length =2= 2

  • 0≤0 \leq startPos, position_i ≤2imes105\leq 2 imes 10^5

  • position_i−1<_{i-1} < position_i_i for any i>0i > 0 (00 indexed)

  • 1≤1 \leq amount_i ≤104\leq 10^4

  • 0≤0 \leq k ≤2imes105\leq 2 imes 10^5

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Maximum Fruits Harvested After at Most K Steps

med
30 min
Try to solve the Maximum Fruits Harvested After at Most K Steps problem.

Statement

Fruits are placed on an infinite number line. You are given a 22D integer array fruits where fruits[i] = [ position_i, amount_i ] indicates that there are amount_i fruits positioned at coordinate position_i. The array fruits is sorted by position_i in strictly ascending order.

You are also given two integers startPos and k. Initially, you are at position startPos. In one step, you can move 11 unit to the left or 11 unit to the right. You can walk at most k steps in total. Whenever you reach a coordinate containing fruits, you collect all fruits at that location. Each fruit can only be collected once.

Return the maximum total number of fruits you can collect.

Constraints:

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

  • fruits[i].length =2= 2

  • 0≤0 \leq startPos, position_i ≤2imes105\leq 2 imes 10^5

  • position_i−1<_{i-1} < position_i_i for any i>0i > 0 (00 indexed)

  • 1≤1 \leq amount_i ≤104\leq 10^4

  • 0≤0 \leq k ≤2imes105\leq 2 imes 10^5