Skip to content

Next Permutation#12

Merged
rihib merged 1 commit intomainfrom
next_permutation
Mar 27, 2025
Merged

Next Permutation#12
rihib merged 1 commit intomainfrom
next_permutation

Conversation

@rihib
Copy link
Copy Markdown
Owner

@rihib rihib commented Aug 8, 2024

Next Permutationを解きました。レビューをお願いいたします。

問題:https://leetcode.com/problems/next-permutation/
言語:Go

すでに解いた方々:
hayashi-ay/leetcode#67
shining-ai/leetcode#58
SuperHotDogCat/coding-interview#8
goto-untrapped/Arai60#12
Exzrgs/LeetCode#7
Mike0121/LeetCode#15

if len(nums) < 2 {
return
}
i := len(nums) - 2
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

iのスコープが少し広いので変数名をつけてほしいと思いましたが、いい名前が思いつきませんでした。すみません。
index_not_ascending_from_backだと長いですね。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

https://en.cppreference.com/w/cpp/algorithm/next_permutation
C++ だと is_sorted_until ですね。いくつかの関数に割ってもいいんじゃないでしょうか。

*/
func nextPermutation_step2(nums []int) {
for i := len(nums) - 1; i > 0; i-- {
if nums[i-1] < nums[i] {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if nums[i-1] >= nums[i] {
    break
}

とすると early return できそうです。
もしかしたらプログラム全体で不等号の向きを < に揃えているかもしれませんが、直線として見たときに nums[i-1] < nums[i] と同じ分かりやすさなのは上記なのかなと思いました。

if nums[i-1] < nums[i] {
candidate, candidateIndex := nums[i], i
for j := i; j < len(nums); j++ {
if nums[j] < candidate && nums[i-1] < nums[j] {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

if nums[i-1] < nums[j] && nums[j] < candidate {

だと位置関係がイメージしやすいのかなと思いました。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

この条件に入る nums[j] は既にcandidate より小さい気がします。

	candidateIndex := i
	for j := i; j < len(nums); j++ {
                if nums[i-1] < nums[j] {
			candidateIndex = j
		}
	}

candidate を使っている箇所を省いて、上記のようにも書けそうです。

for i := len(nums) - 1; i > 0; i-- {
if nums[i-1] < nums[i] {
candidate, candidateIndex := nums[i], i
for j := i; j < len(nums); j++ {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

細かいかもしれませんが、
for j := i + 1; j < len(nums); j++ {
でもいいかもしれません。その上の条件で、j が i の場合はもう見ているのかなと思いました。

return
}
}
sort.Slice(nums, func(a, b int) bool {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ここに入るのは、次の順列が全要素昇順のパターンというふうに理解しています。
そうだとしたら、そのパターンだけここで処理していることが分かるようにコメントなりがあると、何のために必要なのかが分かりやすくなるのかなと思いました。

@rihib rihib merged commit 91120bd into main Mar 27, 2025
@rihib rihib deleted the next_permutation branch March 27, 2025 09:24
rihib added a commit that referenced this pull request Mar 31, 2025
rihib added a commit that referenced this pull request Mar 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants