-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 209. Minimum Size Subarray Sum #51
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?
Conversation
| ```py | ||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| if sum(nums) < target: |
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.
これ必要ですか?
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.
レビューありがとうございます。
自分は必要だと認識しています。
前提として、min_len = min(min_len, x)でtarget以上を作れるsubarrayの最小長を取得しております。
ここでmin_lenの初期値について考えると、minをとってmin_lenを更新しているために、
min_lenとして更新されうる値の最大より大きい値を設定しておく必要があります。
また、min_lenが一度も更新されなかった場合は、target以上を作れないということで、0を返す必要があります。
というわけで、一番初めに「すべてのnumsを足してもtarget以上を作れない(=min_lenが一度も更新されない)」ケースを弾いておくことで、すべてのnumsを足した数をmin_lenの初期値として扱っているというのが意図です。
上記の認識が違う、あるいは違和感を持った点が解消されていないようでしたら、
具体的な箇所をご指摘お願いします。
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| window_sum = 0 | ||
| window_start = 0 | ||
| min_len = inf |
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.
math.inf とするか、from math import inf あたりはあっていいかなと思いました
| for window_end, num in enumerate(nums): | ||
| window_sum += num |
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: これは完全な好みなのですが、あとで nums[window_start] と index でアクセスしているのもあって、window_sum += nums[window_end] の方が統一感と、end の要素にアクセスしている感じがあって好きです。
| window_sum -= nums[window_start] | ||
| window_start += 1 | ||
|
|
||
| if isinf(min_len): |
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.
これ私も意図的によくやりますし、どちらがいいとは言いづらいのですが、isinf って -inf ともマッチするんですよね。min_len == inf の方が一応厳密ではあるかと思いますがこのくらいのサイズのプログラムならどちらでもいいかもしれません。
https://docs.python.org/3/library/math.html#math.isinf
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.
-infもTrueと返してしまうこと知りませんでした。コメントありがとうございます。
Return True if x is a positive or negative infinity,
https://docs.python.org/3/library/math.html#math.isinf
であれば、min_len == infがいいですね。
| if sum(nums) < target: | ||
| return 0 | ||
|
|
||
| min_len = sum(nums) |
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.
上のコメントにも関連するのですが、「取りうる最大の長さよりも大きい」という意味合いなら、len(nums) を取ったほうが自然な気がします。
今回の制約からは外れますが、0 < x < 1 の float を取るような入力が来ると動かないと思います。
ただその場合 sum(nums) == target になるようなケースを処理できないので
if sum(nums) < target:
return 0
if sum(nums) == target:
return len(nums)
...とするか、もしくは min_len = len(nums) + 1 と初期化しても良いと思います。この場合はちょっとコメントでも欲しくなりますね。
あるいは、型の違いはあれど math.inf でもよいと思います。
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.
レビューありがとうございます。
上のコメントにも関連するのですが、「取りうる最大の長さよりも大きい」という意味合いなら、len(nums) を取ったほうが自然な気がします。
これ今気づきました。おっしゃる通りですね。
今回の制約からは外れますが、0 < x < 1 の float を取るような入力が来ると動かないと思います。
おっしゃる通りですね。
min_lenの初期化にnumsの値を使ってしまっていることがそもそもよくなかったので、
min_len = len(nums) + 1がよいかと思いました。ご提案ありがとうございます。
209. Minimum Size Subarray Sum