Skip to content

Practice Maximize Win From Two Segments

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2555
LeetCode Problem

Maximize Win From Two Segments

Problem Statement

There are some prizes on the X-axis. You are given an integer array prizePositions that is sorted in non-decreasing order, where prizePositions[i] is the position of the ith prize. There could be different prizes at the same position on the line. You are also given an integer k. You are allowed to select two segments with integer endpoints. The length of each segment must be k. You will collect all prizes whose position falls within at least one of the two selected segments (including the endpoints of the segments). The two selected segments may intersect. Return the maximum number of prizes you can win if you choose the two segments optimally.

Source: Maximize Win From Two Segments on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
prizePositions = [1,1,2,2,3,3,5], k = 2
Output
7

# Example 2

Input
prizePositions = [1,2,3,4], k = 0
Output
2

Constraints

1 <= prizePositions.length <= 10^5
1 <= prizePositions[i] <= 10^9
0 <= k <= 10^9
prizePositions is sorted in non-decreasing order.

Related LeetCode Problems

Practice more problems similar to Maximize Win From Two Segments.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.