↓Skip to main content
  1. LeetCode/

LeetCode 2555: Maximize Win From Two Segments

·2 mins· ·
LeetCode Medium Sliding-Window Binary-Search Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-01-13

  • Submissions: Accepted (first try)

Approach
#

Overlapping segments are never better than non-overlapping ones, so assume the two segments are one left, one right, disjoint.

  1. Sliding window: the right end is prize right and left is the first prize with position >= prizePositions[right] - k, giving right - left + 1 prizes.
  2. The other segment must lie entirely left of left, using only the first left prizes. Keep best_before[i] = the most one segment can collect among the first i prizes.
  3. The answer is max(right - left + 1 + best_before[left]).

best_before is filled in during the same slide: best_before[right + 1] = max(best_before[right], this segment).

My Solution
#

class Solution:
    def maximizeWin(self, prizePositions: List[int], k: int) -> int:
        n = len(prizePositions)
        best_before = [0] * (n + 1)                # best single segment among the first i prizes
        left = ans = 0
        for right, p in enumerate(prizePositions):
            while p - prizePositions[left] > k:
                left += 1
            cur = right - left + 1
            ans = max(ans, cur + best_before[left])     # second segment left of `left`
            best_before[right + 1] = max(best_before[right], cur)
        return ans

Complexity
#

Time O(n), space O(n).

Mistakes to Avoid
#

  • Adding the two individually best segments: they may collect the same prizes twice.
  • best_before[left] covers “the first left prizes”, excluding left itself, so it never overlaps the current segment.

Interview Explanation
#

The two segments can be taken as disjoint, one left of the other. A sliding window gives how many prizes a segment ending at each prize collects, and an array records the best single segment among the first i prizes. The current segment starts at left, so the other must lie left of it, and the answer is the max of this segment plus best_before[left]. Time O(n).

Related

LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially
LeetCode 3634: Minimum Removals to Balance Array
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 3634 note: after sorting, what remains is always a contiguous block; for each minimum nums[left], a right-only pointer finds how far it can extend, and everything outside is removed
LeetCode 2779: Maximum Beauty of an Array After Applying Operation
·2 mins
LeetCode Medium Sliding-Window Sorting Binary-Search Array
LeetCode 2779 note: each number can become anything in [x - k, x + k], so a group can become one value ⇔ max - min <= 2k; sort and find the longest such window
LeetCode 2831: Find the Longest Equal Subarray
·2 mins
LeetCode Medium Sliding-Window Hash-Table Binary-Search Array
LeetCode 2831 note: list the positions of each value and slide a window over that list; the deletions needed are the span of positions minus the number of copies
LeetCode 209: Minimum Size Subarray Sum (Sliding Window)
·1 min
LeetCode Daily Medium Array Sliding-Window Two-Pointers Prefix-Sum Binary-Search
Sliding window with two pointers; minimize subarray length where sum ≥ target.