Skip to content

Commit 0367382

Browse files
Merge pull request #14 from codewithme-py/feat/minimum-size-subarray-sum-0209
Feat/minimum size subarray sum 0209
2 parents 504715a + 718ae73 commit 0367382

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

problems_cache.json

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19183,5 +19183,115 @@
1918319183
"Delayed Count of Equal Elements",
1918419184
"Medium",
1918519185
"delayed-count-of-equal-elements"
19186+
],
19187+
"4216": [
19188+
"Weighted Word Mapping",
19189+
"Easy",
19190+
"weighted-word-mapping"
19191+
],
19192+
"4137": [
19193+
"Number of Prefix Connected Groups",
19194+
"Medium",
19195+
"number-of-prefix-connected-groups"
19196+
],
19197+
"4062": [
19198+
"House Robber V",
19199+
"Medium",
19200+
"house-robber-v"
19201+
],
19202+
"4209": [
19203+
"Palindromic Path Queries in a Tree",
19204+
"Hard",
19205+
"palindromic-path-queries-in-a-tree"
19206+
],
19207+
"4212": [
19208+
"Toggle Light Bulbs",
19209+
"Easy",
19210+
"toggle-light-bulbs"
19211+
],
19212+
"4217": [
19213+
"First Element with Unique Frequency",
19214+
"Medium",
19215+
"first-element-with-unique-frequency"
19216+
],
19217+
"4221": [
19218+
"Longest Almost-Palindromic Substring",
19219+
"Medium",
19220+
"longest-almost-palindromic-substring"
19221+
],
19222+
"4204": [
19223+
"Maximum Subarray XOR with Bounded Range",
19224+
"Hard",
19225+
"maximum-subarray-xor-with-bounded-range"
19226+
],
19227+
"4170": [
19228+
"Total Distance to Type a String Using One Finger",
19229+
"Medium",
19230+
"total-distance-to-type-a-string-using-one-finger"
19231+
],
19232+
"4224": [
19233+
"Find the Score Difference in a Game",
19234+
"Medium",
19235+
"find-the-score-difference-in-a-game"
19236+
],
19237+
"4226": [
19238+
"Check Digitorial Permutation",
19239+
"Medium",
19240+
"check-digitorial-permutation"
19241+
],
19242+
"4044": [
19243+
"Maximum Bitwise XOR After Rearrangement",
19244+
"Medium",
19245+
"maximum-bitwise-xor-after-rearrangement"
19246+
],
19247+
"4065": [
19248+
"Count Sequences to K",
19249+
"Hard",
19250+
"count-sequences-to-k"
19251+
],
19252+
"4250": [
19253+
"Maximum Requests Without Violating the Limit",
19254+
"Medium",
19255+
"maximum-requests-without-violating-the-limit"
19256+
],
19257+
"4231": [
19258+
"Smallest Pair With Different Frequencies",
19259+
"Easy",
19260+
"smallest-pair-with-different-frequencies"
19261+
],
19262+
"4013": [
19263+
"Merge Close Characters",
19264+
"Medium",
19265+
"merge-close-characters"
19266+
],
19267+
"4219": [
19268+
"Minimum Operations to Make Array Parity Alternating",
19269+
"Medium",
19270+
"minimum-operations-to-make-array-parity-alternating"
19271+
],
19272+
"4239": [
19273+
"Sum of K-Digit Numbers in a Range",
19274+
"Hard",
19275+
"sum-of-k-digit-numbers-in-a-range"
19276+
],
19277+
"4229": [
19278+
"Trim Trailing Vowels",
19279+
"Easy",
19280+
"trim-trailing-vowels"
19281+
],
19282+
"4024": [
19283+
"Minimum Cost to Split into Ones",
19284+
"Medium",
19285+
"minimum-cost-to-split-into-ones"
19286+
],
19287+
"4161": [
19288+
"Minimum Bitwise OR From Grid",
19289+
"Medium",
19290+
"minimum-bitwise-or-from-grid"
19291+
],
19292+
"4002": [
19293+
"Count Subarrays With K Distinct Integers",
19294+
"Hard",
19295+
"count-subarrays-with-k-distinct-integers"
1918619296
]
1918719297
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def minSubArrayLen(self, target: int, nums: list[int]) -> int:
3+
"""
4+
Given an array of positive integers nums and a positive integer target,
5+
return the minimal length of a contiguous subarray of which the sum is
6+
greater than or equal to target.
7+
If there is no such subarray, return 0.
8+
"""
9+
left_mark, current_sum, min_length = 0, 0, float('inf')
10+
for right_mark in range(len(nums)):
11+
current_sum += nums[right_mark]
12+
while current_sum >= target:
13+
min_length = min(min_length, right_mark - left_mark + 1)
14+
current_sum -= nums[left_mark]
15+
left_mark += 1
16+
return 0 if min_length == float('inf') else min_length
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pytest
2+
3+
from solutions.minimum_size_subarray_sum_0209 import Solution
4+
5+
6+
@pytest.mark.parametrize('target, nums, expected', [
7+
(7, [2, 3, 1, 2, 4, 3], 2),
8+
(4, [1, 4, 4], 1),
9+
(1, [1, 1, 1, 1, 1, 1, 1, 1], 1),
10+
(11, [1, 1, 1, 1, 1], 0)
11+
])
12+
def test_min_subarray_len(target: int, nums: list[int], expected: int) -> None:
13+
sol = Solution()
14+
assert sol.minSubArrayLen(target, nums) == expected

0 commit comments

Comments
 (0)