Skip to content

Conversation

@TORUS0818
Copy link
Owner

- 空間計算量:O(1)

```python
class Solution:

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分探索

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

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)

Comment on lines +24 to +27
if min_len == sys.maxsize:
return 0
else:
return min_len

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
Copy link

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]
Copy link

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

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.

5 participants