Skip to content

Conversation

@akmhmgc
Copy link
Owner

@akmhmgc akmhmgc commented Oct 4, 2025

解いた問題

209. Minimum Size Subarray Sum

使用言語

Ruby

次に解く問題

https://leetcode.com/problems/permutations/

@akmhmgc akmhmgc added the ruby label Oct 4, 2025
# @return {Integer}
def min_sub_array_len(target, nums)
min_size = Float::INFINITY
prefix_sum = 0

Choose a reason for hiding this comment

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

prefix_sum というよりは subarray_sum かなと感じました。

# @param {Integer[]} nums
# @return {Integer}
def min_sub_array_len(target, nums)
min_size = Float::INFINITY

Choose a reason for hiding this comment

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

初期値は難しいところですが選択肢の一つとして以下のパターンもありだとは思いました。

NOT_FOUND = nums.size + 1
min_size = NOT_FOUND
...
min_size == NOT_FOUND ? 0 : min_size

end
prefix_sum += num
end
min_size == Float::INFINITY ? 0 : min_size
Copy link

Choose a reason for hiding this comment

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

いいと思います。
別の書き方としては、while trueで二つのポインタを回すような書き方もあると思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

Rubyらしいというのもありますが、必ずrightは1ずつ動くのでわざわざ自分で+1する必要もないかな、と思いこの書き方しています。

ただ、ポインタを使った方がsliding windowらしいのでそれもアリかもしれません。
コメントありがとうございます。

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants