Conversation
potrue
reviewed
Oct 11, 2025
| end | ||
| nil | ||
| end | ||
| rfind_first_greater_than = lambda do |target| |
There was a problem hiding this comment.
一応ここは二分探索が使えますね。(pivot_indexの一つ右からnumsの最後までは降順にソートされているので)
potrue
reviewed
Oct 11, 2025
| right -= 1 | ||
| end | ||
| end | ||
| rfind_first_ascending = lambda do |
There was a problem hiding this comment.
右から見ていったときにはascendingではなくdescendingなのでちょっとややこしい気がします。(firstという単語のニュアンスでは右から見ている感じなので)
参考までに、c++ではis_sorted_untilという関数があったりします。そこから名前を取って、rfind_is_sorted_untilとかでもいいかもしれません。
あと、個人的にはnums.size - 1から初めてnums[i]とnums[i-1]の比較をしたいかもしれません。
Owner
Author
There was a problem hiding this comment.
コメントありがとうございます!
is_sorted_until良いですね。
tokuhirat
reviewed
Oct 13, 2025
| nums.reverse! | ||
| return | ||
| end | ||
| swap_index = rfind_first_greater_than.call(nums[pivot_index]) |
There was a problem hiding this comment.
rfind_first_greater_than を使わずに以下のようにも書けると思いました。
swap_index = nums.rindex { |num| num > nums[pivot_index] }http://docs.ruby-lang.org/ja/latest/class/Array.html#I_RINDEX
折に触れてドキュメントを見ると発見があるかもしれません。
tokuhirat
reviewed
Oct 13, 2025
| nums[pivot_index], nums[swap_index] = nums[swap_index], nums[pivot_index] | ||
| reverse.call(pivot_index + 1, nums.size - 1) | ||
| end | ||
| ``` |
There was a problem hiding this comment.
lambda の定義の間には1行空行があった方が読みやすいと感じました。
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.
解いた問題
31. Next Permutation
使用言語
Ruby
次に解く問題
8. String to Integer (atoi)