-
Notifications
You must be signed in to change notification settings - Fork 0
209. Minimum Size Subarray Sum #43
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
| # @return {Integer} | ||
| def min_sub_array_len(target, nums) | ||
| min_size = Float::INFINITY | ||
| prefix_sum = 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.
prefix_sum というよりは subarray_sum かなと感じました。
| # @param {Integer[]} nums | ||
| # @return {Integer} | ||
| def min_sub_array_len(target, nums) | ||
| min_size = Float::INFINITY |
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.
初期値は難しいところですが選択肢の一つとして以下のパターンもありだとは思いました。
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 |
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.
いいと思います。
別の書き方としては、while trueで二つのポインタを回すような書き方もあると思いました。
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.
Rubyらしいというのもありますが、必ずrightは1ずつ動くのでわざわざ自分で+1する必要もないかな、と思いこの書き方しています。
ただ、ポインタを使った方がsliding windowらしいのでそれもアリかもしれません。
コメントありがとうございます。
解いた問題
209. Minimum Size Subarray Sum
使用言語
Ruby
次に解く問題
https://leetcode.com/problems/permutations/