g2501_2600.s2555_maximize_win_from_two_segments.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java21 Show documentation
Show all versions of leetcode-in-java21 Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
package g2501_2600.s2555_maximize_win_from_two_segments;
// #Medium #Array #Binary_Search #Sliding_Window
// #2023_08_19_Time_5_ms_(90.10%)_Space_55.4_MB_(45.54%)
/**
* 2555 - Maximize Win From Two Segments\.
*
* Medium
*
* 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 _the **maximum** number of prizes you can win if you choose the two segments optimally_.
*
* **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.
*
* **Constraints:**
*
* * 1 <= prizePositions.length <= 105
* * 1 <= prizePositions[i] <= 109
* * 0 <= k <= 109
* * `prizePositions` is sorted in non-decreasing order.
**/
public class Solution {
public int maximizeWin(int[] a, int k) {
int j = 0;
int i;
int n = a.length;
int[] dp = new int[n + 1];
int ans = 0;
for (i = 0; i < a.length; i++) {
while (a[i] - a[j] > k) {
j++;
}
dp[i + 1] = Math.max(dp[i], i - j + 1);
ans = Math.max(ans, dp[j] + i - j + 1);
}
return ans;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy