diff --git a/src/problem/mod.rs b/src/problem/mod.rs index b432d61..32f26cd 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -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; \ No newline at end of file +mod p303_range_sum_query_immutable; +mod p1793_maximum_score_of_a_good_subarray; \ No newline at end of file diff --git a/src/problem/p1793_maximum_score_of_a_good_subarray.rs b/src/problem/p1793_maximum_score_of_a_good_subarray.rs new file mode 100644 index 0000000..09b6754 --- /dev/null +++ b/src/problem/p1793_maximum_score_of_a_good_subarray.rs @@ -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, 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)); + } +}