20240427 Finished

This commit is contained in:
jackfiled 2024-04-27 09:20:30 +08:00
parent 4de10fa2f3
commit 3ba7be1c29
2 changed files with 48 additions and 1 deletions

View File

@ -114,3 +114,4 @@ mod p68_text_justification;
mod p125_valid_palindrome; mod p125_valid_palindrome;
mod p392_is_subsequence; mod p392_is_subsequence;
mod p167_two_sum_ii_input_array_is_sorted; mod p167_two_sum_ii_input_array_is_sorted;
mod p209_minimum_size_subarray_sum;

View File

@ -0,0 +1,46 @@
/**
* [209] Minimum Size Subarray Sum
*/
pub struct Solution {}
// submission codes start here
impl Solution {
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
let length = nums.len();
let (mut start, mut end) = (0, 0);
let mut result = i32::MAX;
let mut sum = 0;
while end < length {
sum += nums[end];
while sum >= target {
result = result.min((end - start + 1) as i32);
sum -= nums[start];
start += 1;
}
end += 1;
}
if result == i32::MAX {
0
} else {
result
}
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_209() {
}
}