-
Notifications
You must be signed in to change notification settings - Fork 0
209. Minimum Size Subarray Sum #46
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
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
209.Minimum-Size-Subarray-Sum
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,21 @@ | ||
| # 209. Minimum Size Subarray Sum | ||
|
|
||
| - sol1.py: 何も見ないで書いた。sliding windowで解ける。O(n)。 | ||
|
|
||
| https://github.com/SuperHotDogCat/coding-interview/pull/31/changes | ||
|
|
||
| 二重のwhile文で書いている。rightの始まりと終わりは確定しているので、個人的にはfor文が良いのではないかと思った。 | ||
| min_length = len(nums) + 1とするのはなるほど。ただ分かりやすさではinfでも悪くはないと思う。 | ||
|
|
||
| https://github.com/olsen-blue/Arai60/pull/50/changes/BASE..1b460a0c97f2ef17efe579db6835cb0623d6cf67#diff-6d4eb2707ed57d8037bfa2e5985424b237a407741a7602ba92b31e090d1cb096 | ||
|
|
||
| https://github.com/naoto-iwase/leetcode/pull/50 | ||
|
|
||
| 二分探索と分割統治法で書く。計算量はO(nlogn)になる | ||
| 二分探索 -> 自分で書くと、leftのindexの処理が彼と異なった。leftを含めるか否か。 | ||
|
|
||
| 二分探索の方は自分でも思いついただろう(今回は人の答えを見たが) | ||
| 分割統治法は人のを見て初めて気づいたので、頭に入れておきたい | ||
|
|
||
|
|
||
|
|
This file contains hidden or 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,18 @@ | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| left = 0 | ||
| current_sum = 0 | ||
| min_subarray_len = float("inf") | ||
| for right, num_right in enumerate(nums): | ||
| current_sum += num_right | ||
| if current_sum < target: | ||
| continue | ||
| while left < len(nums) and current_sum - nums[left] >= target: | ||
| current_sum -= nums[left] | ||
| left += 1 | ||
| min_subarray_len = min(min_subarray_len, right - left + 1) | ||
|
|
||
| return 0 if math.isinf(min_subarray_len) else min_subarray_len |
This file contains hidden or 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,15 @@ | ||
| import itertools | ||
| import bisect | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| left = 0 | ||
| min_subarray_len = float("inf") | ||
| prefix_sum = list(itertools.accumulate(nums)) | ||
| for left, num_left in enumerate(nums): | ||
| right = bisect.bisect_left(prefix_sum, target + prefix_sum[left] - num_left) | ||
| if right < len(nums): | ||
| min_subarray_len = min(min_subarray_len, right - left + 1) | ||
| return 0 if math.isinf(min_subarray_len) else min_subarray_len |
This file contains hidden or 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,42 @@ | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| def devide_and_conquer(left, right): | ||
| if left > right: | ||
| return float("inf") | ||
| if left == right: | ||
| return float("inf") if nums[left] < target else 1 | ||
| mid = left + (right - left) // 2 | ||
| return min( | ||
| devide_and_conquer(left, mid), | ||
| devide_and_conquer(mid + 1, right), | ||
| cross_middle(left, right, mid, target), | ||
| ) | ||
|
|
||
| def cross_middle(left, right, mid, target): | ||
| if mid >= len(nums): | ||
| return float("inf") | ||
| left_cross = mid | ||
| right_cross = mid + 1 | ||
| min_length = float("inf") | ||
| current_sum = nums[left_cross] + nums[right_cross] | ||
| while right_cross <= right: | ||
| while current_sum < target and left_cross > left: | ||
|
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. 数直線上に一直線に並べたい感覚があります。 while current_sum < target and left < left_cross:好みの問題かもしれません。
Owner
Author
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. 不当号の向きが同じ方が分かりやすいのかもしれません。頭に留めておきます。 |
||
| left_cross -= 1 | ||
| current_sum += nums[left_cross] | ||
| if current_sum >= target: | ||
| min_length = min(min_length, right_cross - left_cross + 1) | ||
| right_cross += 1 | ||
| if right_cross > right: | ||
| continue | ||
| current_sum += nums[right_cross] | ||
| while current_sum >= target and left_cross < mid: | ||
| current_sum -= nums[left_cross] | ||
| left_cross += 1 | ||
|
|
||
| return min_length | ||
|
|
||
| min_length = devide_and_conquer(0, len(nums) - 1) | ||
| return 0 if math.isinf(min_length) else min_length | ||
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.
Python では原則整数のオーバーフローがないため、オーバーフローを回避するためのこの書き方はしなくてよいと思います。
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.
個人的には慣習という意味でも残しておきたいです。
また、メモリの消費が小さくなる場合もあると考えます。