-
Notifications
You must be signed in to change notification settings - Fork 0
46. Permutations #47
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
Open
tom4649
wants to merge
2
commits into
main
Choose a base branch
from
46.Permutations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
46. Permutations #47
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # 46. Permutations | ||
|
|
||
| https://leetcode.com/problems/permutations/description/ | ||
|
|
||
| 見たことあるやつだと思いながら自分では書けず、調べた | ||
| next permutationというアルゴリズムを学んだ | ||
| 時間計算量 O(N N!)、空間計算量 O(1) | ||
| 後からdeepcopyを使う必要はないことに気がついたので書き直す | ||
|
|
||
| - mutableなオブジェクトを含むオブジェクト:深いコピー | ||
| - immutableなオブジェクトだけを含むオブジェクト:浅いコピー | ||
|
|
||
|
|
||
| itertoolsにも実装がある: sol2.py | ||
|
|
||
| https://github.com/naoto-iwase/leetcode/blob/0046-permutations/0046-permutations/0046-permutations.md#%E5%AE%9F%E8%A3%852 | ||
|
|
||
| https://github.com/mamo3gr/arai60/blob/46_permutations/46_permutations/memo.md | ||
|
|
||
| https://leetcode.com/problems/permutations/solutions/18239/a-general-approach-to-backtracking-questions-in-java-subsets-permutations-combination-sum-palindrome-partioning/ | ||
|
|
||
| バックトラック:「探索中に一時的に状態を変更し、再帰から戻るときに必ず元に戻す DFS」 | ||
|
|
||
| おそらくこの問題の題意はこれだろう。 | ||
| スタックと再帰関数で実装する | ||
|
|
||
| sol3.py | ||
| 人のコードを一通り見た後に空で書くとこうなった | ||
| 時間:O(n n!)、空間:ざっくりO(n n!) | ||
| - geminiにrevisedさせるとビットマスクを提案された | ||
|
|
||
| sol4.py | ||
| geminiにrevisedさせると順序一定のためにsortを提案された | ||
| 値に重複があるときはindexで持つべきだろう | ||
|
|
||
|
|
||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import copy | ||
|
|
||
|
|
||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def next_permutation(sequence): | ||
| for i in range(len(sequence) - 1, 0, -1): | ||
| if sequence[i] <= sequence[i - 1]: | ||
| continue | ||
| for j in range(len(sequence) - 1, i - 1, -1): | ||
| if sequence[j] >= sequence[i - 1]: | ||
| sequence[i - 1], sequence[j] = sequence[j], sequence[i - 1] | ||
| sequence[i:] = reversed(sequence[i:]) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| nums_sorted = sorted(nums) | ||
| permutations = [copy.deepcopy(nums_sorted)] | ||
| while next_permutation(nums_sorted): | ||
| permutations.append(copy.deepcopy(nums_sorted)) | ||
|
|
||
| return permutations | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import copy | ||
|
|
||
|
|
||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def next_permutation(sequence): | ||
| for i in range(len(sequence) - 1, 0, -1): | ||
| if sequence[i] <= sequence[i - 1]: | ||
| continue | ||
| for j in range(len(sequence) - 1, i - 1, -1): | ||
| if sequence[j] >= sequence[i - 1]: | ||
| sequence[i - 1], sequence[j] = sequence[j], sequence[i - 1] | ||
| sequence[i:] = reversed(sequence[i:]) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| nums_sorted = sorted(nums) | ||
| permutations = [nums_sorted.copy()] | ||
| while next_permutation(nums_sorted): | ||
| permutations.append(nums_sorted.copy()) | ||
|
|
||
| return permutations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import copy | ||
|
|
||
|
|
||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def next_permutation(sequence): | ||
| n = len(sequence) | ||
| pivot = -1 | ||
|
|
||
| for i in range(n - 1, 0, -1): | ||
| if sequence[i] > sequence[i - 1]: | ||
| pivot = i - 1 | ||
| break | ||
|
|
||
| if pivot == -1: | ||
| return False | ||
|
|
||
| for j in range(n - 1, pivot, -1): | ||
| if sequence[j] > sequence[pivot]: | ||
| sequence[pivot], sequence[j] = sequence[j], sequence[pivot] | ||
| sequence[pivot + 1 :] = reversed(sequence[pivot + 1 :]) | ||
| return True | ||
|
|
||
| nums_sorted = sorted(nums) | ||
| permutations = [nums_sorted.copy()] | ||
| while next_permutation(nums_sorted): | ||
| permutations.append(nums_sorted.copy()) | ||
|
|
||
| return permutations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import itertools | ||
|
|
||
|
|
||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| return list(itertools.permutations(nums)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| permutations = [] | ||
| frontier = [([], set(nums))] | ||
|
|
||
| while frontier: | ||
| fixed, not_used = frontier.pop() | ||
| if len(fixed) == len(nums): | ||
| permutations.append(fixed) | ||
| continue | ||
| for num in not_used: | ||
| frontier.append((fixed + [num], not_used - {num})) | ||
|
|
||
| return permutations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| n = len(nums) | ||
| permutations = [] | ||
| frontier = [([], (1 << n) - 1)] | ||
|
|
||
| while frontier: | ||
| fixed, mask = frontier.pop() | ||
| if len(fixed) == n: | ||
| permutations.append(fixed) | ||
| continue | ||
| m = mask | ||
| while m: | ||
| lsb = m & -m | ||
| i = lsb.bit_length() - 1 | ||
| m ^= lsb | ||
| frontier.append((fixed + [nums[i]], mask ^ lsb)) | ||
|
|
||
| return permutations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| permutations = [] | ||
| fixed = [] | ||
| not_used = set(nums) | ||
|
|
||
| def traverse(): | ||
| if len(fixed) == len(nums): | ||
| permutations.append(fixed.copy()) | ||
| return | ||
| for num in list(not_used): | ||
| not_used.remove(num) | ||
| fixed.append(num) | ||
| traverse() | ||
| fixed.pop() | ||
| not_used.add(num) | ||
|
|
||
| traverse() | ||
| return permutations |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| permutations = [] | ||
| fixed = [] | ||
| not_used = set(nums) | ||
|
|
||
| def traverse(): | ||
| if len(fixed) == len(nums): | ||
| permutations.append(fixed.copy()) | ||
| return | ||
| for num in sorted(list(not_used)): | ||
| not_used.remove(num) | ||
| fixed.append(num) | ||
| traverse() | ||
| fixed.pop() | ||
| not_used.add(num) | ||
|
|
||
| traverse() | ||
| return permutations |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
実質外側のループを回し終えたあとの処理が、ループの内側に書かれている点に違和感を感じました。一度ループを抜けてから、この処理を書いたほうが、読み手にとって理解しやすくなると思います。
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.
なるほど、採用させていただきました。pivot変数でiを保存し、その値に応じた分岐を行うようにしました