2555. Maximize Win From Two Segments Medium
1/**
2 * [2555] Maximize Win From Two Segments
3 *
4 * 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 i^th prize. There could be different prizes at the same position on the line. You are also given an integer k.
5 * 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.
6 *
7 * For example if k = 2, you can choose segments [1, 3] and [2, 4], and you will win any prize <font face="monospace">i</font> that satisfies 1 <= prizePositions[i] <= 3 or 2 <= prizePositions[i] <= 4.
8 *
9 * Return the maximum number of prizes you can win if you choose the two segments optimally.
10 *
11 * <strong class="example">Example 1:
12 *
13 * Input: prizePositions = [1,1,2,2,3,3,5], k = 2
14 * Output: 7
15 * Explanation: In this example, you can win all 7 prizes by selecting two segments [1, 3] and [3, 5].
16 *
17 * <strong class="example">Example 2:
18 *
19 * Input: prizePositions = [1,2,3,4], k = 0
20 * Output: 2
21 * Explanation: For this example, one choice for the segments is [3, 3] and [4, 4], and you will be able to get 2 prizes.
22 *
23 *
24 * Constraints:
25 *
26 * 1 <= prizePositions.length <= 10^5
27 * 1 <= prizePositions[i] <= 10^9
28 * 0 <= k <= 10^9
29 * prizePositions is sorted in non-decreasing order.
30 *
31 *
32 * <style type="text/css">.spoilerbutton {display:block; border:dashed; padding: 0px 0px; margin:10px 0px; font-size:150%; font-weight: bold; color:#000000; background-color:cyan; outline:0;
33 * }
34 * .spoiler {overflow:hidden;}
35 * .spoiler > div {-webkit-transition: all 0s ease;-moz-transition: margin 0s ease;-o-transition: all 0s ease;transition: margin 0s ease;}
36 * .spoilerbutton[value="Show Message"] + .spoiler > div {margin-top:-500%;}
37 * .spoilerbutton[value="Hide Message"] + .spoiler {padding:5px;}
38 * </style>
39 *
40 */
41pub struct Solution {}
42
43// problem: https://leetcode.com/problems/maximize-win-from-two-segments/
44// discuss: https://leetcode.com/problems/maximize-win-from-two-segments/discuss/?currentPage=1&orderBy=most_votes&query=
45
46// submission codes start here
47
48impl Solution {
49 pub fn maximize_win(prize_positions: Vec<i32>, k: i32) -> i32 {
50 0
51 }
52}
53
54// submission codes end
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59
60 #[test]
61 fn test_2555() {
62 }
63}
64
Back
© 2025 bowen.ge All Rights Reserved.