-
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 2104: Sum of Subarray Ranges
- Loading branch information
Showing
3 changed files
with
107 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
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,24 @@ | ||
pub mod monotonic_stack; | ||
|
||
pub trait Solution { | ||
fn sub_array_ranges(nums: Vec<i32>) -> i64; | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Solution; | ||
|
||
pub fn run<S: Solution>() { | ||
let test_cases = [ | ||
(&[1, 2, 3] as &[_], 4), | ||
(&[1, 3, 3], 4), | ||
(&[4, -2, -3, 4, 1], 59), | ||
(&[-69, -70, -56, -83, 63], 694), | ||
(&[0, 29, -52, -45, 41, -86, -9, 96, 73, -77], 6077), | ||
]; | ||
|
||
for (nums, expected) in test_cases { | ||
assert_eq!(S::sub_array_ranges(nums.to_vec()), expected); | ||
} | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
src/problem_2104_sum_of_subarray_ranges/monotonic_stack.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 ------------------------------------------------------ // | ||
|
||
use std::cmp::Ordering; | ||
|
||
impl Solution { | ||
pub fn sub_array_ranges(nums: Vec<i32>) -> i64 { | ||
let mut min_stack = Vec::<(i32, u32)>::new(); | ||
let mut prev_min_length = 1; | ||
let mut max_stack = Vec::<(i32, u32)>::new(); | ||
let mut iter = nums.into_iter(); | ||
let mut prev_max_length = 1; | ||
let mut prev = iter.next().unwrap(); | ||
let mut sum = 0; | ||
let mut result = 0; | ||
|
||
for num in iter { | ||
match num.cmp(&prev) { | ||
Ordering::Less => { | ||
max_stack.push((prev, prev_max_length)); | ||
prev_max_length = 1; | ||
|
||
sum += u64::from((prev - num) as u32) * u64::from(prev_min_length); | ||
prev_min_length += 1; | ||
|
||
while let Some(&(top, top_length)) = min_stack.last() { | ||
if num <= top { | ||
sum += u64::from((top - num) as u32) * u64::from(top_length); | ||
prev_min_length += top_length; | ||
min_stack.pop(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
Ordering::Equal => { | ||
prev_min_length += 1; | ||
prev_max_length += 1; | ||
} | ||
Ordering::Greater => { | ||
min_stack.push((prev, prev_min_length)); | ||
prev_min_length = 1; | ||
|
||
sum += u64::from((num - prev) as u32) * u64::from(prev_max_length); | ||
prev_max_length += 1; | ||
|
||
while let Some(&(top, top_length)) = max_stack.last() { | ||
if num >= top { | ||
sum += u64::from((num - top) as u32) * u64::from(top_length); | ||
prev_max_length += top_length; | ||
max_stack.pop(); | ||
} else { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
prev = num; | ||
result += sum; | ||
} | ||
|
||
result as _ | ||
} | ||
} | ||
|
||
// ------------------------------------------------------ snip ------------------------------------------------------ // | ||
|
||
impl super::Solution for Solution { | ||
fn sub_array_ranges(nums: Vec<i32>) -> i64 { | ||
Self::sub_array_ranges(nums) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
#[test] | ||
fn test_solution() { | ||
super::super::tests::run::<super::Solution>(); | ||
} | ||
} |