Skip to content

Conversation

@akmhmgc
Copy link
Owner

@akmhmgc akmhmgc commented Oct 11, 2025

解いた問題

31. Next Permutation

使用言語

Ruby

次に解く問題

8. String to Integer (atoi)

@akmhmgc akmhmgc added the ruby label Oct 11, 2025
end
nil
end
rfind_first_greater_than = lambda do |target|
Copy link

@potrue potrue Oct 11, 2025

Choose a reason for hiding this comment

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

一応ここは二分探索が使えますね。(pivot_indexの一つ右からnumsの最後までは降順にソートされているので)

right -= 1
end
end
rfind_first_ascending = lambda do
Copy link

Choose a reason for hiding this comment

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

右から見ていったときにはascendingではなくdescendingなのでちょっとややこしい気がします。(firstという単語のニュアンスでは右から見ている感じなので)
参考までに、c++ではis_sorted_untilという関数があったりします。そこから名前を取って、rfind_is_sorted_untilとかでもいいかもしれません。
あと、個人的にはnums.size - 1から初めてnums[i]とnums[i-1]の比較をしたいかもしれません。

Copy link
Owner Author

Choose a reason for hiding this comment

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

コメントありがとうございます!
is_sorted_until良いですね。

nums.reverse!
return
end
swap_index = rfind_first_greater_than.call(nums[pivot_index])

Choose a reason for hiding this comment

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

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
折に触れてドキュメントを見ると発見があるかもしれません。

nums[pivot_index], nums[swap_index] = nums[swap_index], nums[pivot_index]
reverse.call(pivot_index + 1, nums.size - 1)
end
```

Choose a reason for hiding this comment

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

lambda の定義の間には1行空行があった方が読みやすいと感じました。

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