Maximum Coins From K Consecutive Bags - Problem

Imagine an infinite treasure hunt along a number line! There are bags at every integer coordinate, and some of these bags contain precious coins. You're given a 2D array coins where each element coins[i] = [l_i, r_i, c_i] tells you that every bag from position l_i to r_i (inclusive) contains exactly c_i coins.

Here's the catch: the coin segments don't overlap, so you have a clear map of where all the treasures are located. Your mission is to find the maximum number of coins you can collect by choosing any k consecutive bags along the number line.

Goal: Return the maximum coins you can obtain from k consecutive bags.
Input: A 2D array of coin segments and an integer k
Output: Maximum coins from k consecutive positions

Input & Output

example_1.py β€” Basic Case
$ Input: coins = [[2,4,3],[5,6,1],[7,9,2]], k = 4
β€Ί Output: 8
πŸ’‘ Note: The optimal window is positions [4,5,6,7] which gives us 3+1+2+2 = 8 coins. Position 4 has 3 coins, position 5 has 1 coin, position 6 has 0 coins (gap), and positions 7 has 2 coins.
example_2.py β€” Overlapping Window
$ Input: coins = [[1,3,2],[4,6,3]], k = 3
β€Ί Output: 6
πŸ’‘ Note: The best window is positions [4,5,6] giving us 3+3+3 = 9 coins, or positions [2,3,4] giving us 2+2+3 = 7 coins. Wait, let me recalculate: [4,5,6] = 3+3+3 = 9.
example_3.py β€” Large Gap
$ Input: coins = [[1,2,5],[100,101,3]], k = 3
β€Ί Output: 10
πŸ’‘ Note: Best window is [1,2,3] with 5+5+0 = 10 coins, or [100,101,102] with 3+3+0 = 6 coins. The first option is better.

Constraints

  • 1 ≀ k ≀ 2 Γ— 104
  • 0 ≀ coins.length ≀ 103
  • coins[i].length == 3
  • 1 ≀ li ≀ ri ≀ 2 Γ— 104
  • 1 ≀ ci ≀ 1000
  • The segments in coins are non-overlapping

Visualization

Tap to expand
πŸ΄β€β˜ οΈ Treasure Hunt Number LineZone A[2,4]: 3 goldZone B[5,6]: 1 goldZone C[7,9]: 2 goldCollection Window (k=4)πŸ† Optimal Position: 8 Gold Coins!πŸ’‘ Key: Only check positions where treasure density changes!
Understanding the Visualization
1
πŸ—ΊοΈ Map the Treasure Zones
Identify all the segments where treasures exist and note their boundaries
2
πŸ“ Mark Critical Points
Focus only on positions where treasure density changes (segment boundaries)
3
πŸƒ Sliding Window Search
Move a collection window of size k across all possible positions
4
πŸ’° Track Maximum Haul
Keep track of the window position that yields the most treasure
Key Takeaway
🎯 Key Insight: Instead of checking every possible position on the infinite line, we only need to examine the critical points where coin densities change (segment boundaries). This reduces an infinite search space to just a few important positions, making the solution efficient and scalable.
Asked in
Google 35 Amazon 28 Microsoft 22 Meta 18
23.8K Views
Medium-High Frequency
~18 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen