Skip to content

Create 46. Permutations.md#14

Merged
Mike0121 merged 6 commits intomainfrom
46.-Permutations
Nov 1, 2025
Merged

Create 46. Permutations.md#14
Mike0121 merged 6 commits intomainfrom
46.-Permutations

Conversation

@Mike0121
Copy link
Copy Markdown
Owner

Copy link
Copy Markdown

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

他の方が書かれている、 nums を inplace に書き換えて permutations を作っていくアルゴリズムも書いてみていただけますか?おそらく、いくつかの言語の標準ライブラリで、そのような実装が使われていると思いますので。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

デバッグ出力を消してからレビュー依頼を出していただけるとありがたいです。

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.

失礼致しました。次回コミット時に合わせて削除いたします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

時間計算量が不必要に大きいように感じます。このアルゴリズムの時間計算量を求めてみていただけますか?

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.

承知しました。各アルゴリズムに対して時間計算量の見積もりをしてみます。

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.

この解法の時間計算量は、if num not in permutation:の部分でO(N)かかってしまっています。問題文にdistinct integersとあるため、setに変換してから判断を行えば、O(1)にできるかと考えました。(ずれたことを言ってたらすみません。)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

こちらも時間計算量を求めてみていただけますか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

デバッグのプリントが残っています。

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.

失礼致しました。次回コミット時に合わせて削除いたします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

permutation_and_remainingでどうでしょう。permutation and remaining (numbers)のイメージです。remainは動詞なので、permutation_and_remainだと変な感じですね。

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.

ありがとうございます。たしかに、品詞は揃えた方が良いですね。permutation_and_remainingにします。

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

正確に言うと、モジュールというよりも、標準ライブラリのことでしょうか?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

List[List[int]]を返していないことに気づきましたか?

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.

ありがとうございます、気がつきませんでした。
実際にprintしてみると、List[Tuple[int]]が返ってきていました。
追記でList[List[int]]を返すコードを書きましたので、よければご確認お願いします。

@liquo-rice
Copy link
Copy Markdown

他の方が書かれている、 nums を inplace に書き換えて permutations を作っていくアルゴリズムも書いてみていただけますか?おそらく、いくつかの言語の標準ライブラリで、そのような実装が使われていると思いますので。

最初の二つがそのアルゴリズムじゃないですか?

@nodchip
Copy link
Copy Markdown

nodchip commented May 12, 2024

他の方が書かれている、 nums を inplace に書き換えて permutations を作っていくアルゴリズムも書いてみていただけますか?おそらく、いくつかの言語の標準ライブラリで、そのような実装が使われていると思いますので。

最初の二つがそのアルゴリズムじゃないですか?

説明足らずで申し訳ありません。

  1. nums をソートする
  2. ループ開始
  3. 後ろから見て昇順になっていない箇所を見つける
  4. 値を入れ替える
  5. 左右反転する
    といった雰囲気のアルゴリズムを指していました。

別の問題の回答ですが、この辺りをイメージしていました。
https://github.com/Exzrgs/LeetCode/compare/arai60-next-permutation
https://github.com/SuperHotDogCat/coding-interview/pull/8/files

@liquo-rice
Copy link
Copy Markdown

他の方が書かれている、 nums を inplace に書き換えて permutations を作っていくアルゴリズムも書いてみていただけますか?おそらく、いくつかの言語の標準ライブラリで、そのような実装が使われていると思いますので。

最初の二つがそのアルゴリズムじゃないですか?

説明足らずで申し訳ありません。

  1. nums をソートする
  2. ループ開始
  3. 後ろから見て昇順になっていない箇所を見つける
  4. 値を入れ替える
  5. 左右反転する
    といった雰囲気のアルゴリズムを指していました。

別の問題の回答ですが、この辺りをイメージしていました。 https://github.com/Exzrgs/LeetCode/compare/arai60-next-permutation https://github.com/SuperHotDogCat/coding-interview/pull/8/files

なるほど、全ての要素がuniqueじゃないなら、このやり方が必要ですよね。LeetCodeにもありました。https://leetcode.com/problems/permutations-ii/description/

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

引数には名前つけた方が分かりやすいと思いました。
startとかどうでしょうか。もっといい名前ありそうですが。

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.

ありがとうございます!start_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.

numsを破壊しているのが少し怖いです。
個人的にはcopyしたものをいじりたいです。

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.

ありがとうございます。
空間計算量が増えてしまうと思い、直接書きました。実際にはコピーを作った方が安全ですね。

Mike0121 added 2 commits May 15, 2024 00:35
デバッグのprint文を削除
各アルゴリズムに時間計算量・空間計算量を追記
標準ライブラリによる解答の返り値をList[List[int]]にするコードを追記
numsをin-placeで書き換える解法を追記
Copy link
Copy Markdown

@nodchip nodchip left a comment

Choose a reason for hiding this comment

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

よいと思います。

@rihib rihib mentioned this pull request Aug 13, 2024
@Mike0121 Mike0121 merged commit 01847e6 into main Nov 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants