sliding window

Leejaegun·2025년 3월 18일

코딩테스트 시리즈

목록 보기
21/49

https://leetcode.com/problems/maximum-subarray/description/

from typing import List

class Solution:
    def minSubArrayLen(self, target: int, nums: List[int]) -> int:
        left = 0
        current_sum = 0
        min_length = float("inf")

        for right in range(len(nums)):
            current_sum += nums[right]

            while current_sum >= target:
                min_length = min(min_length, right - left + 1)
                current_sum -= nums[left]
                left += 1

        return min_length if min_length != float("inf") else 0

# 예제 실행
solution = Solution()
print(solution.minSubArrayLen(7, [2, 3, 1, 2, 4, 3]))  # 출력: 2 ([4,3]이 최소 길이)
profile
Lee_AA

0개의 댓글