20240911 finished.

This commit is contained in:
jackfiled 2024-09-11 11:10:12 +08:00
parent d03c1058c1
commit d249f777dd
2 changed files with 54 additions and 1 deletions

View File

@ -232,3 +232,4 @@ mod p3177_find_the_maximum_length_of_a_good_subsequence_ii;
mod p977_squares_of_a_sorted_array; mod p977_squares_of_a_sorted_array;
mod p2181_merge_nodes_in_between_zeros; mod p2181_merge_nodes_in_between_zeros;
mod p2552_count_increasing_quadruplets; mod p2552_count_increasing_quadruplets;
mod p2555_maximize_win_from_two_segments;

View File

@ -0,0 +1,52 @@
/**
* [2555] Maximize Win From Two Segments
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn maximize_win(prize_positions: Vec<i32>, k: i32) -> i32 {
let n = prize_positions.len();
let mut dp = vec![0;n + 1];
let mut result = 0;
for i in 0..n {
let j = Self::binary_search(&prize_positions, prize_positions[i] - k);
result = result.max(i - j + 1 + dp[j]);
dp[i + 1] = dp[i].max(i - j + 1);
}
result as i32
}
fn binary_search(array: &Vec<i32>, target: i32) -> usize {
let (mut left, mut right) = (0, array.len());
while left < right {
let middle = left + (right - left) / 2;
if array[middle] < target {
left = middle + 1;
} else {
right = middle;
}
}
left
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_2555() {
assert_eq!(7, Solution::maximize_win(vec![1,1,2,2,3,3,5], 2));
assert_eq!(2, Solution::maximize_win(vec![1,2,3,4], 0));
}
}