Timeline
Timeline
2025-11-11
init
Sliding window
Problem:
Sliding window:
12345678910111213141516171819202122232425262728 | using std::vector;class Solution {public: int minSubArrayLen(int target, vector<int>& nums) { int n = nums.size(); int left = 0; int curr_sum = 0; int res = INT_MAX; for (int right = 0; right < n; ++right) { curr_sum += nums[right]; // When the sum within the window is >= target, try to shrink the left boundary. while (curr_sum >= target) { res = std::min(res, right - left + 1); // Record the minimum length. curr_sum -= nums[left]; left++; } } return (res == INT_MAX ? 0 : res); }}; |
