-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 46. Permutations #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
| candidate.pop() | ||
|
|
||
| return permutations | ||
| ``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
読みやすいです。
分かりやすさとのトレードオフはありそうですが、下のようにindexを引数にとる感じでヘルパー関数を定義してバックトラッキングを行うようなやり方もよく見ます。構成してきた順列と使っていない要素を一緒に一つの配列で管理できるようになり、if num in candidate:で弾く必要がなくなるので、多少効率的になるかもしれません。
def permuteHelper(i):
if i == len(nums): # len(nums) - 1 でも動くかも
permutations.append(nums[:])
return
for j in range(i, len(nums)):
nums[i], nums[j] = nums[j], nums[i]
permuteHelper(i + 1)
nums[i], nums[j] = nums[j], nums[i]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビュー、代替案のご提示ありがとうございます。
- 0番目を固定して、0番目に入れ替えられるパターンを列挙する
- 0,1番目を固定して、1番目以降の要素で入れ替えられるパターンを列挙する
- 0,1,2番目を.....
という流れで組み合わせを列挙できる規則を利用するのですね。
気づけていなかったです。ありがとうございます!
| candidates = [] | ||
| for num in nums: | ||
| candidates.append([num]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
candidates = [[]] でも良いと思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
candidateに何かを入れておかねばという意識が先行してnumを入れていましたが、おっしゃる通り[]を入れておくでも問題ないですね。
|
|
||
| ### 感想 | ||
| - これまで使っているnumsの要素が入っているという意味でusedと命名していたが、candidate の方が実体に近いので変えるべき | ||
| - itertools.permutationsは読みづらかった。というか理解できていないので、時間をおいて読む |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/tokuhirat/LeetCode/pull/50/files#diff-f980f45b4d4f94c41556977f060a371872f422650416425386f1322e52deba58R88
itertools.permutations のコードにコメントをつけてみたので、ご参考になればと思います。
| return | ||
| for num in nums: | ||
| if num in used: | ||
| continue |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
num in used の時間計算量が線形のため、全体の計算量が O(n^2n!) になるように思いました。 used 以外に set を持たせると、時間計算量が O(nn!) に落ちると思いました。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
レビューありがとうございます。
if num in usedの考慮が漏れていました。
おっしゃる通りO(n^2 * n!)が正しいですね。
46. Permutations