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.
For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize i that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.
Return themaximum number of prizes you can win if you choose the two segments optimally.
Input: prizePositions =[1,2,3,4], k =0Output: 2Explanation: For this example,**one choice**for the segments is[3,3] and [4,4], and you will be able to get 2 prizes.
Since the array is sorted, we can use a sliding window to find the maximum number of prizes that can be collected in a segment of length k ending at each position. To maximize the total, we can combine two such segments, possibly overlapping, by keeping track of the best result for the left segment as we move the right segment.
classSolution {
public:int maximizeWin(vector<int>& prizePositions, int k) {
int n = prizePositions.size();
vector<int> pre(n+1, 0);
int ans =0, l =0;
for (int r =0; r < n; ++r) {
while (prizePositions[r] - prizePositions[l] > k) ++l;
int cnt = r - l +1;
pre[r+1] = max(pre[r], cnt);
}
l =0;
for (int r =0; r < n; ++r) {
while (prizePositions[r] - prizePositions[l] > k) ++l;
int cnt = r - l +1;
ans = max(ans, cnt + pre[l]);
}
return ans;
}
};
funcmaximizeWin(prizePositions []int, kint) int {
n:= len(prizePositions)
pre:= make([]int, n+1)
ans, l:=0, 0forr:=0; r < n; r++ {
forprizePositions[r]-prizePositions[l] > k {
l++ }
cnt:=r-l+1ifpre[r] > cnt {
pre[r+1] = pre[r]
} else {
pre[r+1] = cnt }
}
l = 0forr:=0; r < n; r++ {
forprizePositions[r]-prizePositions[l] > k {
l++ }
cnt:=r-l+1ifans < cnt+pre[l] {
ans = cnt+pre[l]
}
}
returnans}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
publicintmaximizeWin(int[] prizePositions, int k) {
int n = prizePositions.length;
int[] pre =newint[n+1];
int ans = 0, l = 0;
for (int r = 0; r < n; ++r) {
while (prizePositions[r]- prizePositions[l]> k) ++l;
int cnt = r - l + 1;
pre[r+1]= Math.max(pre[r], cnt);
}
l = 0;
for (int r = 0; r < n; ++r) {
while (prizePositions[r]- prizePositions[l]> k) ++l;
int cnt = r - l + 1;
ans = Math.max(ans, cnt + pre[l]);
}
return ans;
}
}
classSolution {
funmaximizeWin(prizePositions: IntArray, k: Int): Int {
val n = prizePositions.size
val pre = IntArray(n+1)
var ans = 0var l = 0for (r in0 until n) {
while (prizePositions[r] - prizePositions[l] > k) l++val cnt = r - l + 1 pre[r+1] = maxOf(pre[r], cnt)
}
l = 0for (r in0 until n) {
while (prizePositions[r] - prizePositions[l] > k) l++val cnt = r - l + 1 ans = maxOf(ans, cnt + pre[l])
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defmaximizeWin(self, prizePositions: list[int], k: int) -> int:
n = len(prizePositions)
pre = [0] * (n+1)
ans = l =0for r in range(n):
while prizePositions[r] - prizePositions[l] > k:
l +=1 cnt = r - l +1 pre[r+1] = max(pre[r], cnt)
l =0for r in range(n):
while prizePositions[r] - prizePositions[l] > k:
l +=1 cnt = r - l +1 ans = max(ans, cnt + pre[l])
return ans
impl Solution {
pubfnmaximize_win(prize_positions: Vec<i32>, k: i32) -> i32 {
let n = prize_positions.len();
letmut pre =vec![0; n+1];
letmut ans =0;
letmut l =0;
for r in0..n {
while prize_positions[r] - prize_positions[l] > k {
l +=1;
}
let cnt = r - l +1;
pre[r+1] = pre[r].max(cnt);
}
l =0;
for r in0..n {
while prize_positions[r] - prize_positions[l] > k {
l +=1;
}
let cnt = r - l +1;
ans = ans.max(cnt + pre[l]);
}
ans asi32 }
}