-
Notifications
You must be signed in to change notification settings - Fork 0
add memo.md #31
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
skypenguins
wants to merge
1
commit into
main
Choose a base branch
from
leetcode/arai60/problem-46
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
add memo.md #31
Changes from all commits
Commits
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,141 @@ | ||
| # 46. Permutations | ||
| - 問題: https://leetcode.com/problems/permutations/ | ||
| - 言語: Python | ||
|
|
||
| ## Step1 | ||
| - 与えられた配列から、普通の順列を配列の配列として返す | ||
| - 順列は樹形図を描いて求める | ||
| - n個の要素がある配列をソートして、小さい値を訪れたらその値を含めずに残りn-1個の要素を小さい順に並べる、これを繰り返す | ||
| - スタックを使った解法が思いつきそうで思いつかず15分以上経っていたので正答を見る | ||
|
|
||
| ### 正答 | ||
| - 節を訪れる途中で何を覚えておくか?を考える | ||
| - 出典: https://leetcode.com/problems/permutations/solutions/993970/python-4-approaches-visuals-time-complex-053h | ||
|
|
||
| 1. 再帰+バックトラック(暗黙的なスタック) | ||
| - N!個の葉に訪れるには、N回(=深さ)の呼び出しが必要 | ||
| - 葉ではpathそのものを append するのではなく、パスのコピー `path[::]` を追加する | ||
| - 結果リストにpathを追加した後でも、そのパスはエイリアス(別名)に対する変更の影響を受けるから | ||
| ```py | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def permute_helper(nums, path=[], result=[]): | ||
| if not nums: | ||
| result.append(path[::]) # path のコピーを result に追加 | ||
| return | ||
|
|
||
| for i in range(len(nums)): | ||
| new_nums = nums[:i] + nums[i+1:] # 順列の定義から | ||
| path.append(nums[i]) | ||
| permute_helper(new_nums, path, result) | ||
| path.pop() # 選択を取り消す(バックトラック) | ||
|
|
||
| return result | ||
|
|
||
| return permute_helper(nums) | ||
| ``` | ||
| - 時間計算量: $O(N*N!)$ | ||
| - 空間計算量: $O(N!)$ | ||
|
|
||
| 2. 再帰+バックトラックなし | ||
| - | ||
| ```py | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| def permute_helper(nums, path=[], result=[]): | ||
| if not nums: | ||
| result.append(path) | ||
| return | ||
|
|
||
| for i in range(len(nums)): | ||
| new_nums = nums[:i] + nums[i+1:] | ||
| new_path = path + [nums[i]] # 毎回新しいリストを生成 | ||
| permute_helper(new_nums, new_path, result) | ||
|
|
||
| return result | ||
|
|
||
| return permute_helper(nums) | ||
| ``` | ||
| - 時間計算量: $O(N*N!)$ | ||
| - 空間計算量: $O(N!)$ | ||
| - 毎回リストを生成するため若干メモリ効率は悪い | ||
|
|
||
| 3. DFS(スタック) | ||
| ```py | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| result = [] | ||
| stack = [(nums, [])] | ||
|
|
||
| while stack: | ||
| nums, path = stack.pop() | ||
| if not nums: | ||
| result.append(path) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. continueを加えた方が良いと思いました |
||
|
|
||
| for i in range(len(nums)): | ||
| new_nums = nums[:i] + nums[i+1:] | ||
| stack.append((new_nums, path + [nums[i]])) | ||
|
|
||
| return result | ||
| ``` | ||
| - 時間計算量: $O(N*N!)$ | ||
| - 空間計算量: $O(N!)$ | ||
|
|
||
| 4. BFS(キュー) | ||
| ```py | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| result = [] | ||
| queue = deque() | ||
| queue.append((nums, [])) | ||
|
|
||
| while queue: | ||
| nums, path = queue.popleft() | ||
| if not nums: | ||
| result.append(path) | ||
|
|
||
| for i in range(len(nums)): | ||
| new_nums = nums[:i] + nums[i+1:] | ||
| queue.append((new_nums, path + [nums[i]])) | ||
|
|
||
| return result | ||
| ``` | ||
| - 時間計算量: $O(N*N!)$ | ||
| - 空間計算量: $O(N!)$ | ||
|
|
||
| ## Step2 | ||
| - 典型コメント集: https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/edit?tab=t.0#heading=h.xe8lirtynkse | ||
|
|
||
| 1. https://github.com/Ryotaro25/leetcode_first60/pull/54 | ||
| - C++ | ||
| - 自分の場合はPythonのスライスを使って重複を防いでいたが、これのStep4ではSetを使っている | ||
| - 個人的にはあまり直観的ではないかなと思った | ||
| - Setの検索は定数時間のため、時間計算量の面では有利? | ||
|
|
||
| 2. 処理系CPythonでのPermutation実装: https://github.com/python/cpython/blob/main/Modules/itertoolsmodule.c#L2725 | ||
| - 出力の長さを指定できる | ||
| - CPythonの実装は「`indices` 配列をスワップして順列を生成する」という全く別の設計で、再帰もバックトラックも使っていないらしい by Claude | ||
|
|
||
| ## Step3 | ||
| - DFS(スタック) | ||
| ```py | ||
| class Solution: | ||
| def permute(self, nums: List[int]) -> List[List[int]]: | ||
| result = [] | ||
| stack = [(nums, [])] | ||
|
|
||
| while stack: | ||
| nums, path = stack.pop() | ||
| if not nums: | ||
| result.append(path) | ||
|
|
||
| for i in range(len(nums)): | ||
| new_nums = nums[:i] + nums[i+1:] | ||
| stack.append((new_nums, path + [nums[i]])) | ||
|
|
||
| return result | ||
| ``` | ||
| - 所要時間 | ||
| - 1回目: 2:57 | ||
| - 2回目: 2:36 | ||
| - 3回目: 3:04 | ||
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.
ここで nums を使うと再代入になるので remaining など他の変数名にした方が良いと思いました
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.
確かに別の変数名の方がわかりやすいですね。ありがとうございます。