-
-
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
ecd0580
added a problem with solution on sliding window.
rohan472000 ccf8634
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d621e2e
added hint for return type and parameter
rohan472000 26fb18a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 36024f6
Update minimum_size_subarray_sum.py
rohan472000 2fa46db
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 21136aa
Update minimum_size_subarray_sum.py
rohan472000 7931904
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c6fabb4
Update minimum_size_subarray_sum.py
rohan472000 94d638f
Update minimum_size_subarray_sum.py
rohan472000 b4d9efc
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7eb87fe
Update minimum_size_subarray_sum.py
rohan472000 06915bf
removed un-necessary docs and added 2 test cases
rohan472000 dfd660c
Rename sliding_window/minimum_size_subarray_sum.py to dynamic_program…
rohan472000 4bc8033
Update minimum_size_subarray_sum.py
rohan472000 7476e78
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 5098fbf
Update minimum_size_subarray_sum.py
rohan472000 a266d4e
Update minimum_size_subarray_sum.py
rohan472000 55f0882
Update minimum_size_subarray_sum.py
rohan472000 b111c31
Update minimum_size_subarray_sum.py
rohan472000 35b2ff2
Update minimum_size_subarray_sum.py
rohan472000 f8c2d67
Update minimum_size_subarray_sum.py
rohan472000 5890611
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 615854f
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 254f88e
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 695605c
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 5e5f528
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 57e5e37
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 f00d071
Update dynamic_programming/minimum_size_subarray_sum.py
rohan472000 34eefad
Update minimum_size_subarray_sum.py
rohan472000 92e2640
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d65430e
Fix
cclauss c20a5b1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] c88303d
Update minimum_size_subarray_sum.py
rohan472000 2c69934
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ae1996c
Update minimum_size_subarray_sum.py
rohan472000 823287e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 36d09a0
Update minimum_size_subarray_sum.py
rohan472000 d3cd162
Update minimum_size_subarray_sum.py
rohan472000 b67b6f7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 1e18958
Update minimum_size_subarray_sum.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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 | ||
>>> minsubarraysum(4, [1, 4, 4]) | ||
1 | ||
>>> minsubarraysum(11, [1, 1, 1, 1, 1, 1, 1, 1]) | ||
0 | ||
""" | ||
|
||
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 @ 24:5.
parser error: error at 23: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 | ||
|
||
|
||
nums = [2, 3, 1, 2, 4, 3] | ||
target = 7 | ||
print(minsubarraysum(target, nums)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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....