#2555

Maximize Win From Two Segments

expert · 1045 · lc medium +32 · verified · 37.5% accepted · 603 likes · top 16%

Description

Prizes are located at positions given by sorted array prizePositions. You may choose two segments of length k on the number line (they may overlap) and collect every prize within either segment (endpoints inclusive). Return the maximum number of prizes collectible with an optimal placement of the two segments.

Example 1:

Input: prizePositions = [1,1,2,2,3,3,5], k = 2
Output: 7
Explanation: In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].

Example 2:

Input: prizePositions = [1,2,3,4], k = 0
Output: 2
Explanation: For this example, one choice for the segments is [3, 3] and [4, 4], and you will be able to get 2 prizes.

Code

1
2
3