Skip to content

Conversation

@Fuminiton
Copy link
Owner

```py
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
if sum(nums) < target:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これ必要ですか?

Copy link
Owner Author

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

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 あたりはあっていいかなと思いました

Comment on lines +20 to +21
for window_end, num in enumerate(nums):
window_sum += num

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):

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

Copy link
Owner Author

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)

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 でもよいと思います。

Copy link
Owner Author

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がよいかと思いました。ご提案ありがとうございます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants