209. Minimum Size Subarray Sum

Hill K·2022년 8월 30일
0

Algorithm

목록 보기
11/11

Example 1

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Example 2

Input: target = 4, nums = [1,4,4]
Output: 1

조건

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

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        total, left, right = 0, 0, 0
        res = len(nums) + 1
        while right < len(nums):
            total += nums[right]
            
            while total >= target:
                res = min(res, right - left + 1)
                total -= nums[left]
                left += 1
            right += 1
            print(total, right, left, res)
        if res <= len(nums):
            return res
        else:
            return 0
profile
안녕하세요

0개의 댓글