Skip to content

Commit 4670417

Browse files
authored
Merge pull request #2473 from Maheshwari-Love/add/solutionn-lc-2555
added solution for lc-2555
2 parents 57c8bc9 + 2b2eff9 commit 4670417

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
---
2+
id: maximiz-win-from-two-segments
3+
title: Maximize Win From Two Segments
4+
sidebar_label: 2555 Maximize Win From Two Segments
5+
6+
tags:
7+
- Binary Search
8+
- Array
9+
- Sorting
10+
- Greedy
11+
description: "This is a solution to the Maximize Win From Two Segments
12+
problem on LeetCode."
13+
---
14+
15+
## Problem Description
16+
17+
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.
18+
19+
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.
20+
21+
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`
22+
Return the maximum number of prizes you can win if you choose the two segments optimally.
23+
24+
### Examples
25+
26+
**Example 1:**
27+
28+
```
29+
30+
Input: prizePositions = [1,1,2,2,3,3,5], k = 2
31+
Output: 7
32+
Explanation: In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
33+
34+
```
35+
36+
**Example 2:**
37+
```
38+
Input: prizePositions = [1,2,3,4], k = 0
39+
Output: 2
40+
Explanation: For this example, one choice for the segments is [3, 3] and [4, 4], and you will be able to get 2 prizes.
41+
42+
```
43+
44+
45+
### Constraints
46+
47+
- `1 <= prizePositions.length <= 10^5`
48+
- `1 <= prizePositions[i] <= 10^9`
49+
50+
51+
### Approach
52+
53+
We define $f[i]$ as the maximum number of prizes that can be obtained by selecting a segment of length $k$ from the first $i$ prizes. Initially, $f[0] = 0$. We define the answer variable as $ans = 0$.
54+
55+
Next, we enumerate the position $x$ of each prize, and use binary search to find the leftmost prize index $j$ such that $prizePositions[j] \geq x - k$. At this point, we update the answer $ans = \max(ans, f[j] + i - j)$, and update $f[i] = \max(f[i - 1], i - j)$.
56+
57+
Finally, we return $ans$.
58+
59+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Where $n$ is the number of prizes.
60+
61+
62+
63+
#### Python3
64+
65+
```python
66+
class Solution:
67+
def maximizeWin(self, prizePositions: List[int], k: int) -> int:
68+
n = len(prizePositions)
69+
f = [0] * (n + 1)
70+
ans = 0
71+
for i, x in enumerate(prizePositions, 1):
72+
j = bisect_left(prizePositions, x - k)
73+
ans = max(ans, f[j] + i - j)
74+
f[i] = max(f[i - 1], i - j)
75+
return ans
76+
```
77+
78+
#### Java
79+
80+
```java
81+
class Solution {
82+
public int maximizeWin(int[] prizePositions, int k) {
83+
int n = prizePositions.length;
84+
int[] f = new int[n + 1];
85+
int ans = 0;
86+
for (int i = 1; i <= n; ++i) {
87+
int x = prizePositions[i - 1];
88+
int j = search(prizePositions, x - k);
89+
ans = Math.max(ans, f[j] + i - j);
90+
f[i] = Math.max(f[i - 1], i - j);
91+
}
92+
return ans;
93+
}
94+
95+
private int search(int[] nums, int x) {
96+
int left = 0, right = nums.length;
97+
while (left < right) {
98+
int mid = (left + right) >> 1;
99+
if (nums[mid] >= x) {
100+
right = mid;
101+
} else {
102+
left = mid + 1;
103+
}
104+
}
105+
return left;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```cpp
113+
class Solution {
114+
public:
115+
int maximizeWin(vector<int>& prizePositions, int k) {
116+
int n = prizePositions.size();
117+
vector<int> f(n + 1);
118+
int ans = 0;
119+
for (int i = 1; i <= n; ++i) {
120+
int x = prizePositions[i - 1];
121+
int j = lower_bound(prizePositions.begin(), prizePositions.end(), x - k) - prizePositions.begin();
122+
ans = max(ans, f[j] + i - j);
123+
f[i] = max(f[i - 1], i - j);
124+
}
125+
return ans;
126+
}
127+
};
128+
```

0 commit comments

Comments
 (0)