-
Notifications
You must be signed in to change notification settings - Fork 0
Create 209. Minimum Size Subarray Sum.md #49
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,85 @@ | ||
| # 209. Minimum Size Subarray Sum | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - 始点を順に走査する。subarray の合計が target 未満の間、終点を +1 していく。while 文を抜けた時に最小値を更新するが、right が末尾に来た場合を除く必要がある。 | ||
| ```python | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| total = 0 | ||
| right = 0 | ||
| min_length = math.inf | ||
| for left in range(len(nums)): | ||
| while total < target and right < len(nums): | ||
| total += nums[right] | ||
| right += 1 | ||
| if total >= target: | ||
| min_length = min(min_length, right - left) | ||
| total -= nums[left] | ||
|
|
||
| if math.isinf(min_length): | ||
| return 0 | ||
| return min_length | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/olsen-blue/Arai60/pull/50/files | ||
| - この問題も right を for 文で走査した方がわかりやすそう。while の条件が正しい間は min_length を更新できるため。 | ||
| ```python | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| total = 0 | ||
| left = 0 | ||
| min_length = math.inf | ||
| for right in range(len(nums)): | ||
| total += nums[right] | ||
| while total >= target: | ||
| min_length = min(min_length, right - left + 1) | ||
| total -= nums[left] | ||
| left += 1 | ||
|
|
||
| if math.isinf(min_length): | ||
| return 0 | ||
| return min_length | ||
| ``` | ||
|
|
||
| - https://github.com/hayashi-ay/leetcode/pull/51/files | ||
| - 2分探索の発想がわからなかったが、累積和を使ってO(n^2)で求める方法をベースにしているとのこと。愚直から変形していく例としては重要と思った。 | ||
| - https://github.com/Mike0121/LeetCode/pull/22/files#r1623412986 | ||
| - min_length の初期値について、まずnumsの合計を確認するというのは良い方法と思った。 | ||
| - 他には、len(nums) + 1 や フラグの変数をおくのもあり。 | ||
| - 今回は10行程度の関数なのでそこまでケアしなくて良いと思ったが選択肢としては持っていたい。 | ||
| - https://docs.python.org/3/library/math.html#math.isinf | ||
| > Return True if x is a positive or negative infinity, and False otherwise. | ||
| - -inf も True になる点に注意。min_length は負にならないので個人的には math.inf で判定しても気にならない。 | ||
| - ユースケースによっては nums が正の数のみ含むことを確認した方が良さそう。 | ||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| import math | ||
|
|
||
|
|
||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| min_length = math.inf | ||
| total = 0 | ||
| left = 0 | ||
| for right in range(len(nums)): | ||
| total += nums[right] | ||
| while total >= target: | ||
| min_length = min(min_length, right - left + 1) | ||
| total -= nums[left] | ||
| left += 1 | ||
|
|
||
| if math.isinf(min_length): | ||
| return 0 | ||
| return min_length | ||
| ``` | ||
|
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. 特に問題ないと思います。
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. ありがとうございます。len(nums)+1はいいですね。最後の比較するところがmin_length == len(nums)+1 だとわかりにくい気がしていて、以下のような感じかなと思っています。 NOT_FOUND = len(nums) + 1
min_length = NOT_FOUND
...
if min_length == NOT_FOUND:
return 0
return min_length |
||
|
|
||
| 2分,2分,2分で3回Accept | ||
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.
Nit:
isinfは-infにも True を返しますね