[LeetCode] 209. Minimum Size Subarray Sum(C++)


Source of the subject: https://leetcode-cn.com/problems/minimum-size-subarray-sum/

Title description

Given a string, you find out which does not contain a repeated character longest substring length.

示例 1:

输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
示例 2:

输入:target = 4, nums = [1,4,4]
输出:1
示例 3:

输入:target = 11, nums = [1,1,1,1,1,1,1,1]
输出:0
 

提示:

1 <= target <= 109
1 <= nums.length <= 105
1 <= nums[i] <= 105


进阶:

如果你已经实现 O(n) 时间复杂度的解法, 请尝试设计一个 O(nlog(n)) 时间复杂度的解法。

General idea

  • Find the longest string that does not contain repeated characters, use a cnt array that records the number of occurrences, through the form of sliding window + double pointer

Sliding window + dual pointer

  • Maintain a variable-length window, keep moving the left and right pointers, and take the longest window length
class Solution {
    
    
public:
    int minSubArrayLen(int target, vector<int>& nums) {
    
    
        int len = nums.size(), left = 0, right = 0;
        int ans = INT_MAX, cnt = 0;
        bool flag = false;
        for(; right < len ; ++right){
    
    
            cnt += nums[right];
            while(cnt >= target){
    
    
                ans = min(ans, right - left + 1);
                cnt -= nums[left];
                ++left;
                flag = true;
            }
        }
        if(!flag)   return 0;
        return ans;
    }
};

Complexity analysis

  • Time complexity: O(n). Both the left and right pointers have moved n times
  • Space complexity: O(1)

Prefix and + binary search

  • Although the complexity of the sliding window is O(n), the advanced topic requires nlogn. Don’t ask, the question is that O(nlogn) may be better than O(n). Thinking of logn can be associated with an ordered array. The time complexity of binary search for elements is O(logn). One way to order the array is to sort directly, and the other is to construct a monotonic sequence by prefixing and constructing a monotonic sequence. The element value in the title must be a positive number, so the prefix and Must be a monotonic sequence
  • Among them, sum[i] represents the sum of elements from nums[0] to nums[i-1]. After getting the prefix sum, for each starting subscript i, the cumulative sum of sum[i, bound] from i to bound (that is, the cumulative sum of consecutive sub-arrays from i to bound) can be searched by binary search, if it is found to satisfy sum [i,bound]>=target, update the minimum length of the sub-array (the length of the sub-array at this time is bound-i + 1)
  • Here you can directly call the stl encapsulated lower_boundvector to directly perform binary search calculations (80% of them are not directly called during the interview), here manually return the index value corresponding to the first element greater than or equal to target
class Solution {
    
    
public:
    int minSubArrayLen(int target, vector<int>& nums) {
    
    
        int len = nums.size(), ans = INT_MAX;
        int sums[len + 1];
        sums[0] = nums[0];
        // sums[0] = 0 意味着前 0 个元素的前缀和为 0
        // sums[1] = A[0] 前 1 个元素的前缀和为 A[0]
        for(int i = 1 ; i <= len ; ++i)  sums[i] = sums[i - 1] + nums[i - 1];
        for(int i = 1 ; i <= len ; ++i){
    
    
            int newTarget = target + sums[i - 1];
            int bound = BinarySearch(newTarget, sums, 0, len);
            if(bound != -1){
    
    
               ans = min(ans, bound - i + 1);
            }
        }
        return ans == INT_MAX ? 0 : ans;
    }
    int BinarySearch(int target, int* nums, int left, int right){
    
    
        while(left <= right){
    
    
            int mid = right + ((left - right) >> 1);
            if(nums[mid] < target)  left = mid + 1;
            else{
    
    
                if(mid == 1 || nums[mid - 1] < target)  return mid;
                else    right = mid - 1;
            }
        }
        return -1;
    }
};

Complexity analysis

  • Time complexity: O(nlogn). n is the length of the array, n cycles * logn is the average complexity of binary search
  • Space complexity: O(n). n is the length of the array, maintain a prefix and array

Guess you like

Origin blog.csdn.net/lr_shadow/article/details/113947699