-
Notifications
You must be signed in to change notification settings - Fork 0
Create 0209-minimum-size-subarray-sum.md #50
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
docto-rin
wants to merge
1
commit into
main
Choose a base branch
from
0209-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.
+98
−0
Open
Changes from all commits
Commits
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
98 changes: 98 additions & 0 deletions
98
0209-minimum-size-subarray-sum/0209-minimum-size-subarray-sum.md
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,98 @@ | ||
| ## Step 1 | ||
|
|
||
| - 問題文 | ||
| - 正整数の配列`nums`と正整数`target`が与えられる。 | ||
| - 和が`target`以上になるような部分配列(subarray)の長さの最小値を返せ。 | ||
| - もしそのようなsubarrayがなければ0を返せ。 | ||
| - 制約: | ||
| - 1 <= target <= 10^9 | ||
| - 1 <= nums.length <= 10^5 | ||
| - 1 <= nums[i] <= 10^4 | ||
|
|
||
| ### 実装1 | ||
|
|
||
| - アルゴリズムの選択 | ||
| - subarrayは連続している必要があるので、これもgreedyに解けたら嬉しい。 | ||
| - ひとまず、和がtarget以上になるまでsubarrayの右端を広げていき、target以上になったら左端を縮めていく。 | ||
| - 実装 | ||
| - for-while、で書くと良さそう。 | ||
| - 計算量 | ||
| - Time: O(n) | ||
| - Space: O(1) | ||
|
|
||
| ```python3 | ||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: list[int]) -> int: | ||
| INF = len(nums) + 1 | ||
| min_length = INF | ||
| left = 0 | ||
| subarray_sum = 0 | ||
|
|
||
| for right in range(len(nums)): | ||
| subarray_sum += nums[right] | ||
| while subarray_sum >= target: | ||
| min_length = min(min_length, right - left + 1) | ||
| subarray_sum -= nums[left] | ||
| left += 1 | ||
|
|
||
| if min_length == INF: | ||
| return 0 | ||
| return min_length | ||
| ``` | ||
|
|
||
| - ここまで6分。 | ||
| - > Follow up: If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log(n)). | ||
| - うーん、いきなりsliding windowでやったから再検討が必要。 | ||
| - まず原点回帰して、最もナイーブには全探索で、O(n^2)となる。 | ||
| - しかし、内側のループは無駄が多そう。 | ||
| - 前からの累積和は単調増加するので、二分探索(lower bound)で初めて和がtarget以上になる点がわかる。 | ||
|
|
||
| ### 実装2 | ||
|
|
||
| - 左ポインタが全探索、右ポインタが二分探索 | ||
| - 計算量 | ||
| - Time: O(nlogn) | ||
| - Space: O(n) | ||
|
|
||
| ```python3 | ||
| import itertools | ||
| import bisect | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: list[int]) -> int: | ||
| INF = len(nums) + 1 | ||
| min_length = INF | ||
|
|
||
| prefix_sum = [0] + list(itertools.accumulate(nums)) | ||
| for left in range(len(nums)): | ||
| right = bisect.bisect_left(prefix_sum, target + prefix_sum[left]) | ||
| if right == len(prefix_sum): | ||
| continue | ||
| min_length = min(min_length, right - left) | ||
|
|
||
| if min_length == INF: | ||
| return 0 | ||
| return min_length | ||
| ``` | ||
|
|
||
| - follow-upは8分ほど。 | ||
| - 今は単元別に学習しているせいで思考が飛躍的だが、本来はこういう方法を経るのが自然な思考回路か。 | ||
|
|
||
| ## Step 2 | ||
|
|
||
| - レビュー by GPT-5 | ||
| - > 実装1で min_length == 1min_length == 1 になったらそれ以上短縮不可なので即 return してもよい。最悪計算量は変わらないが、平均では効くことがある。 | ||
| - よく思いつくなぁ。 | ||
| - [コメント集](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.p6d6fndbrthh) | ||
| - https://discord.com/channels/1084280443945353267/1225849404037009609/1253256524609097801 | ||
| - > なんか、right += 1 と prefix_sum += nums[right] が分裂しているのがちょっと気になります。 | ||
| - 別の問題で自分も同様のコメントをいただいたことがある。 | ||
| - 例えば境界条件の処理などがやりたいときに、インクリメントの遅延で実現するのは好ましくないだろう。(他の選択肢もあるはず) | ||
| - https://discord.com/channels/1084280443945353267/1322513618217996338/1352304493097652315 | ||
| - numsに0も許容する -> prefix_sumが広義単調増加になっても、問題ない。 | ||
| - target以上なmin_lengthならbisect_left、targetより大きいmin_lengthならbisect_rightで広義もいけてそう。 | ||
|
|
||
| ## Step 3 | ||
|
|
||
| [実装1](#実装1) | ||
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.
半分に割って分割統治することを繰り返すという方法があります。中央をまたぐ場合とまたがない場合に分類します。