20240319 Finished

This commit is contained in:
jackfiled 2024-03-19 14:12:26 +08:00
parent 691a3e0bb2
commit 925e9051e2
2 changed files with 50 additions and 1 deletions

View File

@ -77,4 +77,5 @@ mod p2789_largest_element_in_an_array_after_merge_operations;
mod p2312_selling_pieces_of_wood;
mod p2684_maximum_number_of_moves_in_a_grid;
mod p310_minimum_height_trees;
mod p303_range_sum_query_immutable;
mod p303_range_sum_query_immutable;
mod p1793_maximum_score_of_a_good_subarray;

View File

@ -0,0 +1,48 @@
/**
* [1793] Maximum Score of a Good Subarray
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn maximum_score(nums: Vec<i32>, k: i32) -> i32 {
let length = nums.len() as i32;
let mut left = k - 1;
let mut right = k + 1;
let mut result = 0;
for i in (0..=nums[k as usize]).rev() {
while left >= 0 && nums[left as usize] >= i {
left -= 1;
}
while right < length && nums[right as usize] >= i {
right += 1;
}
result = result.max((right - left - 1) * i);
if left == -1 && right == length {
break;
}
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1793() {
assert_eq!(15, Solution::maximum_score(vec![1,4,3,7,4,5], 3));
}
}