-
Notifications
You must be signed in to change notification settings - Fork 0
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
| - 空間計算量:O(1) | ||
|
|
||
| ```python | ||
| class Solution: |
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.
とても読みやすかったです。
| - https://docs.python.org/ja/3.13/library/dis.html | ||
| - https://github.com/hayashi-ay/leetcode/pull/51 | ||
|
|
||
| 2分探索 |
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.
累積和を作った後は、自分は愚直に左から見ていきました。
2分探索の発想はなかったので勉強になります。
| ```python | ||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| min_len = sys.maxsize |
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.
取りうる最大の値、という意味で sys.maxsize を使うべきかというかについては議論の余地があると思います。個人的にはここなら math.inf を使いたくなりますね。
このあたりの議論を想定しながら書いています。
huyfififi/coding-challenges#25 (comment)
tokuhirat/LeetCode#28 (comment)
tokuhirat/LeetCode#22 (comment)
| if min_len == sys.maxsize: | ||
| return 0 | ||
| else: | ||
| return 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.
先に return しているので else 不要かと思います。
if min_len == sys.maxsize:
return 0
return min_len| ```python | ||
| class Solution: | ||
| def minSubArrayLen(self, target: int, nums: List[int]) -> int: | ||
| left = 0 |
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: left, right はセットなのでループの真上の行で宣言したい気持ちになりました。
|
|
||
| prefix_sum = [0] * (len(nums) + 1) | ||
| for i in range(1, len(prefix_sum)): | ||
| prefix_sum[i] = prefix_sum[i - 1] + nums[i - 1] |
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.
この3行は prefix_sum = [0] + list(itertools.accumulate(nums)) と書くこともできるようです。
python なら一行で書けそうだなと思い、gemini に聞いてみました。
https://docs.python.org/3/library/itertools.html#itertools.accumulate
https://leetcode.com/problems/minimum-size-subarray-sum/description/