Skip to content

Commit

Permalink
Minimum Size Subarray Sum
Browse files Browse the repository at this point in the history
  • Loading branch information
denis-sukhoverkhov committed Jul 20, 2024
1 parent d76536a commit cba7ab5
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
13 changes: 13 additions & 0 deletions python/leetcode/Minimum Path Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import List


class Solution:
def minPathSum(self, grid: List[List[int]]) -> int:
pass


if __name__ == "__main__":
obj = Solution()

grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]]
assert obj.minPathSum(grid) == 7
45 changes: 45 additions & 0 deletions python/leetcode/Minimum Size Subarray Sum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from typing import List


class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
p1, p2 = 0, 0

curr_sum = 0
min_len = float('inf')
while p1 < len(nums):

if curr_sum < target and p2 >= len(nums):
break

if curr_sum < target and p2 < len(nums):
curr_sum += nums[p2]
p2 += 1
continue

min_len = min(p2 - p1, min_len)

curr_sum -= nums[p1]
p1 += 1

return min_len if min_len != float('inf') else 0


if __name__ == "__main__":
obj = Solution()

target = 15
nums = [5, 1, 3, 5, 10, 7, 4, 9, 2, 8]
assert obj.minSubArrayLen(target, nums) == 2

target = 11
nums = [1, 2, 3, 4, 5]
assert obj.minSubArrayLen(target, nums) == 3

target = 11
nums = [1, 1, 1, 1, 1, 1, 1, 1]
assert obj.minSubArrayLen(target, nums) == 0

target = 7
nums = [2, 3, 1, 2, 4, 3]
assert obj.minSubArrayLen(target, nums) == 2

0 comments on commit cba7ab5

Please sign in to comment.