Class Solution
java.lang.Object
g2501_2600.s2555_maximize_win_from_two_segments.Solution
2555 - Maximize Win From Two Segments.<p>Medium</p>
<p>There are some prizes on the <strong>X-axis</strong>. You are given an integer array <code>prizePositions</code> that is <strong>sorted in non-decreasing order</strong> , where <code>prizePositions[i]</code> is the position of the <code>i<sup>th</sup></code> prize. There could be different prizes at the same position on the line. You are also given an integer <code>k</code>.</p>
<p>You are allowed to select two segments with integer endpoints. The length of each segment must be <code>k</code>. 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.</p>
<ul>
<li>For example if <code>k = 2</code>, you can choose segments <code>[1, 3]</code> and <code>[2, 4]</code>, and you will win any prize i that satisfies <code>1 <= prizePositions[i] <= 3</code> or <code>2 <= prizePositions[i] <= 4</code>.</li>
</ul>
<p>Return <em>the <strong>maximum</strong> number of prizes you can win if you choose the two segments optimally</em>.</p>
<p><strong>Example 1:</strong></p>
<p><strong>Input:</strong> prizePositions = [1,1,2,2,3,3,5], k = 2</p>
<p><strong>Output:</strong> 7</p>
<p><strong>Explanation:</strong></p>
<p>In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].</p>
<p><strong>Example 2:</strong></p>
<p><strong>Input:</strong> prizePositions = [1,2,3,4], k = 0</p>
<p><strong>Output:</strong> 2</p>
<p><strong>Explanation:</strong></p>
<p>For this example, <strong>one choice</strong> for the segments is <code>[3, 3]</code> and <code>[4, 4],</code> and you will be able to get <code>2</code> prizes.</p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= prizePositions.length <= 10<sup>5</sup></code></li>
<li><code>1 <= prizePositions[i] <= 10<sup>9</sup></code></li>
<li><code>0 <= k <= 10<sup>9</sup></code></li>
<li><code>prizePositions</code> is sorted in non-decreasing order.</li>
</ul>
-
Constructor Summary
Constructors -
Method Summary
-
Constructor Details
-
Solution
public Solution()
-
-
Method Details
-
maximizeWin
public int maximizeWin(int[] a, int k)
-