Skip to content

Commit 34219d1

Browse files
committed
leetcode.com 209. Minimum Size Subarray Sum
문제 링크: https://leetcode.com/problems/minimum-size-subarray-sum
1 parent 056238a commit 34219d1

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import sys
2+
from typing import List
3+
4+
5+
class Solution:
6+
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
7+
if len(nums) < 2:
8+
if target <= nums[0]:
9+
return 1
10+
return 0
11+
12+
answer = sys.maxsize
13+
left, right = 0, 0
14+
sm = nums[0]
15+
16+
while left <= right and right < len(nums):
17+
if sm >= target:
18+
answer = min(answer, right + 1 - left)
19+
sm -= nums[left]
20+
left += 1
21+
else: # sm < target
22+
right += 1
23+
if right >= len(nums):
24+
break
25+
sm += nums[right]
26+
27+
if answer == sys.maxsize:
28+
return 0
29+
30+
return answer
31+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from unittest import TestCase
2+
from main import Solution
3+
4+
class TestSolution(TestCase):
5+
def test1_min_sub_array_len(self):
6+
sln = Solution()
7+
self.assertEqual(2, sln.minSubArrayLen(target = 7, nums = [2,3,1,2,4,3]))

0 commit comments

Comments
 (0)