Maximum Coins From K Consecutive Bags - Problem
There are an infinite amount of bags on a number line, one bag for each coordinate. Some of these bags contain coins.
You are given a 2D array coins, where coins[i] = [l_i, r_i, c_i] denotes that every bag from l_i to r_i (inclusive) contains c_i coins. The segments that coins contain are non-overlapping.
You are also given an integer k. Return the maximum amount of coins you can obtain by collecting k consecutive bags.
Input & Output
Example 1 — Basic Case
$
Input:
coins = [[1,5,10], [6,7,1], [8,10,3]], k = 4
›
Output:
40
💡 Note:
The best 4 consecutive bags are positions 1-4, each containing 10 coins: 10 + 10 + 10 + 10 = 40 coins
Example 2 — Single Segment
$
Input:
coins = [[1,3,5]], k = 2
›
Output:
10
💡 Note:
Any 2 consecutive bags within the segment give 5 + 5 = 10 coins
Example 3 — Cross Multiple Segments
$
Input:
coins = [[1,2,1], [4,5,3]], k = 3
›
Output:
4
💡 Note:
Best window spans positions 3-5: bag 3 has 0 coins (no segment covers it), bag 4 has 3 coins, bag 5 has 3 coins = 6 total
Constraints
- 1 ≤ coins.length ≤ 105
- 1 ≤ li ≤ ri ≤ 105
- 1 ≤ ci ≤ 1000
- 1 ≤ k ≤ 2 × 105
- The segments are non-overlapping
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code