Skip to content

Commit

Permalink
Add problem 1793: Maximum Score of a Good Subarray
Browse files Browse the repository at this point in the history
  • Loading branch information
EFanZh committed Apr 8, 2024
1 parent b5ccf35 commit 621a4dc
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,7 @@ pub mod problem_1787_make_the_xor_of_all_segments_equal_to_zero;
pub mod problem_1790_check_if_one_string_swap_can_make_strings_equal;
pub mod problem_1791_find_center_of_star_graph;
pub mod problem_1792_maximum_average_pass_ratio;
pub mod problem_1793_maximum_score_of_a_good_subarray;
pub mod problem_1796_second_largest_digit_in_a_string;
pub mod problem_1798_maximum_number_of_consecutive_values_you_can_make;
pub mod problem_1800_maximum_ascending_subarray_sum;
Expand Down
82 changes: 82 additions & 0 deletions src/problem_1793_maximum_score_of_a_good_subarray/iterative.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
pub struct Solution;

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl Solution {
pub fn maximum_score(nums: Vec<i32>, k: i32) -> i32 {
let k = k as u32 as usize;
let mut result = nums[k] as u32;
let mut left = k;
let mut left_height = result;
let mut right = k;
let mut right_height = result;

loop {
let width = (right - left) as u32;

if left_height < right_height {
result = result.max(right_height * width);
right += 1;

if let Some(&next_right_height) = nums.get(right) {
let next_right_height = next_right_height as u32;

right_height = right_height.min(next_right_height);
} else {
loop {
result = result.max(left_height * (right as u32 - left as u32));
left = left.wrapping_sub(1);

if let Some(&next_left_height) = nums.get(left) {
left_height = left_height.min(next_left_height as _);
} else {
break;
}
}

break;
}
} else {
result = result.max(left_height * width);
left = left.wrapping_sub(1);

if let Some(&next_left_height) = nums.get(left) {
let next_left_height = next_left_height as u32;

left_height = left_height.min(next_left_height);
} else {
loop {
result = result.max(right_height * (right as u32).wrapping_sub(left as _));
right += 1;

if let Some(&next_right_height) = nums.get(right) {
right_height = right_height.min(next_right_height as _);
} else {
break;
}
}

break;
}
}
}

result as _
}
}

// ------------------------------------------------------ snip ------------------------------------------------------ //

impl super::Solution for Solution {
fn maximum_score(nums: Vec<i32>, k: i32) -> i32 {
Self::maximum_score(nums, k)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_solution() {
super::super::tests::run::<super::Solution>();
}
}
21 changes: 21 additions & 0 deletions src/problem_1793_maximum_score_of_a_good_subarray/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
pub mod iterative;

pub trait Solution {
fn maximum_score(nums: Vec<i32>, k: i32) -> i32;
}

#[cfg(test)]
mod tests {
use super::Solution;

pub fn run<S: Solution>() {
let test_cases = [
((&[1, 4, 3, 7, 4, 5] as &[_], 3), 15),
((&[5, 5, 4, 5, 4, 1, 1, 1], 0), 20),
];

for ((nums, k), expected) in test_cases {
assert_eq!(S::maximum_score(nums.to_vec(), k), expected);
}
}
}

0 comments on commit 621a4dc

Please sign in to comment.