Open
Conversation
nodchip
reviewed
Apr 18, 2026
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) |
There was a problem hiding this comment.
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)| return float("inf") | ||
| if left == right: | ||
| return float("inf") if nums[left] < target else 1 | ||
| mid = left + (right - left) // 2 |
There was a problem hiding this comment.
Python では原則整数のオーバーフローがないため、オーバーフローを回避するためのこの書き方はしなくてよいと思います。
mid = (left + right) // 2
Owner
Author
There was a problem hiding this comment.
個人的には慣習という意味でも残しておきたいです。
また、メモリの消費が小さくなる場合もあると考えます。
| min_length = float("inf") | ||
| current_sum = nums[left_cross] + nums[right_cross] | ||
| while right_cross <= right: | ||
| while current_sum < target and left_cross > left: |
There was a problem hiding this comment.
数直線上に一直線に並べたい感覚があります。
while current_sum < target and left < left_cross:好みの問題かもしれません。
Owner
Author
There was a problem hiding this comment.
不当号の向きが同じ方が分かりやすいのかもしれません。頭に留めておきます。
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
https://leetcode.com/problems/minimum-size-subarray-sum/