Conversation
nodchip
left a comment
There was a problem hiding this comment.
他の方が書かれている、 nums を inplace に書き換えて permutations を作っていくアルゴリズムも書いてみていただけますか?おそらく、いくつかの言語の標準ライブラリで、そのような実装が使われていると思いますので。
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
失礼致しました。次回コミット時に合わせて削除いたします。
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
時間計算量が不必要に大きいように感じます。このアルゴリズムの時間計算量を求めてみていただけますか?
There was a problem hiding this comment.
承知しました。各アルゴリズムに対して時間計算量の見積もりをしてみます。
There was a problem hiding this comment.
この解法の時間計算量は、if num not in permutation:の部分でO(N)かかってしまっています。問題文にdistinct integersとあるため、setに変換してから判断を行えば、O(1)にできるかと考えました。(ずれたことを言ってたらすみません。)
競技プロ就活部PR用/46. Permutations.md
Outdated
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
失礼致しました。次回コミット時に合わせて削除いたします。
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
permutation_and_remainingでどうでしょう。permutation and remaining (numbers)のイメージです。remainは動詞なので、permutation_and_remainだと変な感じですね。
There was a problem hiding this comment.
ありがとうございます。たしかに、品詞は揃えた方が良いですね。permutation_and_remainingにします。
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
正確に言うと、モジュールというよりも、標準ライブラリのことでしょうか?
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
ありがとうございます、気がつきませんでした。
実際にprintしてみると、List[Tuple[int]]が返ってきていました。
追記でList[List[int]]を返すコードを書きましたので、よければご確認お願いします。
最初の二つがそのアルゴリズムじゃないですか? |
説明足らずで申し訳ありません。
別の問題の回答ですが、この辺りをイメージしていました。 |
なるほど、全ての要素がuniqueじゃないなら、このやり方が必要ですよね。LeetCodeにもありました。https://leetcode.com/problems/permutations-ii/description/ |
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
引数には名前つけた方が分かりやすいと思いました。
startとかどうでしょうか。もっといい名前ありそうですが。
There was a problem hiding this comment.
ありがとうございます!start_indexとかで良いかもしれません。
競技プロ就活部PR用/46. Permutations.md
Outdated
There was a problem hiding this comment.
numsを破壊しているのが少し怖いです。
個人的にはcopyしたものをいじりたいです。
There was a problem hiding this comment.
ありがとうございます。
空間計算量が増えてしまうと思い、直接書きました。実際にはコピーを作った方が安全ですね。
デバッグのprint文を削除 各アルゴリズムに時間計算量・空間計算量を追記 標準ライブラリによる解答の返り値をList[List[int]]にするコードを追記
numsをin-placeで書き換える解法を追記
Create 50. Pow(x, n).md
https://leetcode.com/problems/permutations/description/