20240714 Finished

This commit is contained in:
jackfiled 2024-07-14 10:54:42 +08:00
parent e0d4f0c442
commit dd7dbc9d53
2 changed files with 48 additions and 1 deletions

View File

@ -172,4 +172,5 @@ mod p39_combination_sum;
mod p22_generate_parentheses;
mod p79_word_search;
mod p108_convert_sorted_array_to_binary_search_tree;
mod p53_maximum_subarray;
mod p53_maximum_subarray;
mod p918_maximum_sum_circular_subarray;

View File

@ -0,0 +1,46 @@
/**
* [918] Maximum Sum Circular Subarray
*/
pub struct Solution {}
// submission codes start here
use std::collections::VecDeque;
impl Solution {
pub fn max_subarray_sum_circular(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut result = nums[0];
let mut queue = VecDeque::new();
let mut pre = nums[0];
queue.push_back((0, pre));
for i in 1..(2 * n) {
while !queue.is_empty() && queue.front().unwrap().0 + n < i {
queue.pop_front();
}
pre += nums[i % n];
result = result.max(pre - queue.front().unwrap().1);
while !queue.is_empty() && queue.back().unwrap().1 >= pre {
queue.pop_back();
}
queue.push_back((i, pre));
}
result
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_918() {
}
}