Maximize Win From Two Segments - Problem

Imagine you're planning a treasure hunt along a straight path! ๐Ÿดโ€โ˜ ๏ธ

You have an array prizePositions that shows where prizes are located on the X-axis, sorted in non-decreasing order. Multiple prizes can exist at the same position. You're also given an integer k representing the length of segments you can choose.

Your mission: Select exactly two segments, each of length k, to maximize the number of prizes you can collect. You win any prize whose position falls within at least one of your chosen segments (including endpoints).

Key points:

  • Each segment has length k with integer endpoints
  • The two segments can overlap - this might be optimal!
  • A prize at position p is won if it falls in segment [a, a+k] when a โ‰ค p โ‰ค a+k

Example: If k = 2, segments [1, 3] and [2, 4] would collect prizes at positions 1, 2, 3, and 4.

Input & Output

example_1.py โ€” Basic Case
$ Input: prizePositions = [1,1,2,2,3,3,5], k = 2
โ€บ Output: 7
๐Ÿ’ก Note: Choose segments [1,3] and [1,3] (overlapping). The first segment covers prizes at positions 1,1,2,2,3,3 (6 prizes), and the second segment covers the same prizes. Since they overlap completely, we get all 7 prizes total (including the prize at position 5 is not optimal, so we choose segments [1,3] and [3,5] to get prizes at 1,1,2,2,3,3,5).
example_2.py โ€” Non-overlapping Optimal
$ Input: prizePositions = [1,2,3,4,5,6,7,8,9,10], k = 2
โ€บ Output: 6
๐Ÿ’ก Note: Choose segments [1,3] and [8,10]. First segment covers positions 1,2,3 (3 prizes) and second segment covers positions 8,9,10 (3 prizes). Total = 6 prizes. This is better than overlapping segments.
example_3.py โ€” All Same Position
$ Input: prizePositions = [1,1,1,1,1], k = 0
โ€บ Output: 5
๐Ÿ’ก Note: With k=0, each segment has length 0, so can only cover one position. Both segments cover position 1, getting all 5 prizes at that position.

Constraints

  • 1 โ‰ค prizePositions.length โ‰ค 105
  • 1 โ‰ค prizePositions[i] โ‰ค 109
  • 0 โ‰ค k โ‰ค 109
  • prizePositions is sorted in non-decreasing order

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Pirate's Treasure Hunt StrategyCoastline๐Ÿ’ฐpos 1๐Ÿ’ฐpos 1๐Ÿ’Žpos 2๐Ÿ’Žpos 2๐Ÿ’pos 3๐Ÿ’pos 3๐Ÿ‘‘pos 5First Net [1,3]Second Net [2,4]๐ŸŽฏ Strategy: Use DP to track best first net + sliding window for second netResult: Collect treasures at positions 1,1,2,2,3,3 + position 5 = 7 total!Optimal Overlap!
Understanding the Visualization
1
Survey the Coast
You see treasures at positions [1,1,2,2,3,3,5] and have two nets of length k=2
2
Plan First Net
Calculate the best position for your first net at each location
3
Deploy Second Net
For each second net position, find the best first net that doesn't interfere
4
Maximize Treasure
Combine both nets optimally to collect maximum treasures
Key Takeaway
๐ŸŽฏ Key Insight: The optimal solution uses dynamic programming to precompute the best first segment ending at each position, then combines it with a sliding window approach to efficiently evaluate all possible second segment positions. This reduces time complexity from O(nยณ) to O(n)!
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28 Apple 22
34.4K Views
Medium-High Frequency
~25 min Avg. Time
1.5K 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