From 3ba7be1c2981fe697f8d81f66a3d2cbac904bfe4 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Sat, 27 Apr 2024 09:20:30 +0800 Subject: [PATCH] 20240427 Finished --- src/problem/mod.rs | 3 +- src/problem/p209_minimum_size_subarray_sum.rs | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/problem/p209_minimum_size_subarray_sum.rs diff --git a/src/problem/mod.rs b/src/problem/mod.rs index a502593..0d4e7c2 100644 --- a/src/problem/mod.rs +++ b/src/problem/mod.rs @@ -113,4 +113,5 @@ mod p28_find_the_index_of_the_first_occurrence_in_a_string; mod p68_text_justification; mod p125_valid_palindrome; mod p392_is_subsequence; -mod p167_two_sum_ii_input_array_is_sorted; \ No newline at end of file +mod p167_two_sum_ii_input_array_is_sorted; +mod p209_minimum_size_subarray_sum; \ No newline at end of file diff --git a/src/problem/p209_minimum_size_subarray_sum.rs b/src/problem/p209_minimum_size_subarray_sum.rs new file mode 100644 index 0000000..c756c02 --- /dev/null +++ b/src/problem/p209_minimum_size_subarray_sum.rs @@ -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 { + 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() { + } +}