Skip to content

31. Next Permutation - #50

Open
akmhmgc wants to merge 1 commit into
mainfrom
31
Open

31. Next Permutation#50
akmhmgc wants to merge 1 commit into
mainfrom
31

Conversation

@akmhmgc

@akmhmgc akmhmgc commented Oct 11, 2025

Copy link
Copy Markdown
Owner

解いた問題

31. Next Permutation

使用言語

Ruby

次に解く問題

8. String to Integer (atoi)

@akmhmgc akmhmgc added the ruby label Oct 11, 2025
Comment thread 31/step3.md
end
nil
end
rfind_first_greater_than = lambda do |target|

@potrue potrue Oct 11, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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

Comment thread 31/step3.md
right -= 1
end
end
rfind_first_ascending = lambda do

Copy link
Copy Markdown

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
Copy Markdown
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良いですね。

Comment thread 31/step3.md
nums.reverse!
return
end
swap_index = rfind_first_greater_than.call(nums[pivot_index])

Copy link
Copy Markdown

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

Comment thread 31/step3.md
nums[pivot_index], nums[swap_index] = nums[swap_index], nums[pivot_index]
reverse.call(pivot_index + 1, nums.size - 1)
end
```

Copy link
Copy Markdown

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.

3 participants