-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add problem 1793: Maximum Score of a Good Subarray
- Loading branch information
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
src/problem_1793_maximum_score_of_a_good_subarray/iterative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |