-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
added a problem with solution on sliding window. #8566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
ecd0580
ccf8634
d621e2e
26fb18a
36024f6
2fa46db
21136aa
7931904
c6fabb4
94d638f
b4d9efc
7eb87fe
06915bf
dfd660c
4bc8033
7476e78
5098fbf
a266d4e
55f0882
b111c31
35b2ff2
f8c2d67
5890611
615854f
254f88e
695605c
5e5f528
57e5e37
f00d071
34eefad
92e2640
d65430e
c20a5b1
c88303d
2c69934
ae1996c
823287e
36d09a0
d3cd162
b67b6f7
1e18958
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def minsubarraysum(target: int, nums: List[int]) -> int: | ||
""" | ||
Returns the length of the shortest contiguous subarray | ||
in nums whose sum is at least target. | ||
|
||
Args: | ||
target (int): The minimum sum that the subarray should have. | ||
nums (List[int]): The array to search for subarrays in. | ||
|
||
Returns: | ||
int: The length of the shortest contiguous subarray | ||
in nums whose sum is at least target. If no such subarray exists, returns 0. | ||
|
||
Examples: | ||
>>> minsubarraysum(7, [2, 3, 1, 2, 4, 3]) | ||
2 | ||
""" | ||
n = len(nums) | ||
if n == 0: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occured while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 145, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 23:5.
parser error: error at 22:4: expected one of (, *, +, -, ..., AWAIT, DEDENT, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
if n == 0:
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occured while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 145, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 20:5.
parser error: error at 19:4: expected one of (, *, +, -, ..., AWAIT, DEDENT, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
if n == 0:
^ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An error occured while parsing the file: Traceback (most recent call last):
File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 145, in parse
reports = lint_file(
^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 19:5.
parser error: error at 18:4: expected one of (, *, +, -, ..., AWAIT, DEDENT, False, NAME, NUMBER, None, True, [, break, continue, lambda, match, not, pass, ~
if n == 0:
^ |
||
return 0 | ||
|
||
left = 0 | ||
right = 0 | ||
curr_sum = 0 | ||
min_len = float("inf") | ||
|
||
while right < n: | ||
curr_sum += nums[right] | ||
while curr_sum >= target: | ||
min_len = min(min_len, right - left + 1) | ||
curr_sum -= nums[left] | ||
left += 1 | ||
right += 1 | ||
|
||
return min_len if min_len != float("inf") else 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a couple more doctests with different numbers and perhaps negative numbers as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah I will do it sooner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@CaedenPH, done....