Skip to content

209. Minimum Size Subarray Sum#46

Open
tom4649 wants to merge 2 commits intomainfrom
209.Minimum-Size-Subarray-Sum
Open

209. Minimum Size Subarray Sum#46
tom4649 wants to merge 2 commits intomainfrom
209.Minimum-Size-Subarray-Sum

Conversation

@tom4649
Copy link
Copy Markdown
Owner

@tom4649 tom4649 commented Apr 17, 2026

Comment thread 209.Minimum-Size-Subarray-Sum/sol1.py Outdated
Comment on lines +11 to +15
if current_sum >= target:
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

early return したほうが見通しが良くなると思います。

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)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

なるほど、採用させていただきました

return float("inf")
if left == right:
return float("inf") if nums[left] < target else 1
mid = left + (right - left) // 2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Python では原則整数のオーバーフローがないため、オーバーフローを回避するためのこの書き方はしなくてよいと思います。

mid = (left + right) // 2

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

個人的には慣習という意味でも残しておきたいです。
また、メモリの消費が小さくなる場合もあると考えます。

min_length = float("inf")
current_sum = nums[left_cross] + nums[right_cross]
while right_cross <= right:
while current_sum < target and left_cross > left:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

数直線上に一直線に並べたい感覚があります。

while current_sum < target and left < left_cross:

好みの問題かもしれません。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

不当号の向きが同じ方が分かりやすいのかもしれません。頭に留めておきます。

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.

2 participants