20240427 Finished
This commit is contained in:
parent
4de10fa2f3
commit
3ba7be1c29
|
@ -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;
|
||||
mod p167_two_sum_ii_input_array_is_sorted;
|
||||
mod p209_minimum_size_subarray_sum;
|
46
src/problem/p209_minimum_size_subarray_sum.rs
Normal file
46
src/problem/p209_minimum_size_subarray_sum.rs
Normal 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() {
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user