-
Notifications
You must be signed in to change notification settings - Fork 0
49.GroupAnagrams #14
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
xbam326
wants to merge
3
commits into
main
Choose a base branch
from
49
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
49.GroupAnagrams #14
Changes from all commits
Commits
Show all changes
3 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,49 @@ | ||
| ## step1 | ||
| - 全ての組み合わせを列挙してその中の一部とすると、strs[i].length <= 100で100!ほどの計算量が必要になり不可なはず | ||
| - 出てきた、文言をアルファベット順に並び替えたものをkeyとして保管しておくといけそう | ||
| - 出てきた文言の並び替えO(MlogM)*文言の個数NでNMlogM = 10^2 * 7 * 10^4 = 7 * 10^6 | ||
| - pythonは1秒で10^8計算できるとする70msくらいでいけそう | ||
| - pythonで文字列をsortした後にlistに1文字ずつ入るのを知らなかった | ||
| - joinでまた文字列に戻した | ||
|
|
||
| ## step2 | ||
| - 流石に出てきた文言の並び替え無しでは解けないと思うのでこれ以上の高速化は無理だと思う | ||
| - `sorted_to_str`よりも良い変数名(`sorted_to_元々/最初の順序のような単語`)が無いか考えたが元々の引数でstrsとなっていたのでstrとした | ||
| - 空文字列が入ってきた場合は今回は問題でOKとなっているが、実際のケースでは使われ方によって変わってくるだろうなと思った | ||
| - 他の人のコードを見る | ||
| - https://github.com/Hiroto-Iizuka/coding_practice/pull/12/files | ||
| - 確かにカウンターでよかった | ||
| - `時間計算量は確かにO(nk) vs O(nk log k)でカウンターの方が有利ですが、カウンターの方は文字列にアルファベットの小文字しか来ないことを前提としており、文字種の拡張性に欠けます。また、kはnに比べたら小さく、sorted(str)もstr.join()もネイティブコードで動くので割と速いので実行時間で見たらおそらく大きな問題(差)にはならず、可読性や拡張性の観点からも私ならソートする方法を採用すると思います。なお重要なのはどちらを選ぶかよりも、こうした多角的な観点で比較検討できるかどうかだと思います。` | ||
| - カウンターの選択肢をもった上で考えたかった | ||
| - 最後は`list(sorted_to_str.values())`でよかった | ||
| - https://github.com/mamo3gr/arai60/pull/12/files | ||
| - `ソートにする。時間計算量は単語数が支配的だろうから、ヒストグラムを作るのがソートよりも速くても、その寄与は小さいことが予想される。空間計算量のオーダーは変わらない(たぶんソートの場合の法が多少有利)。コードの簡潔さはソートの方が有利そうで、ここが個人的に大きい(ヒストグラムをタプルで作って…みたいなのが面倒)。`こんな風な選択肢をもった上での意思決定をしたかった | ||
| - `frozenset`というものがあるみたい。普通のsetと異なり、削除ができないが辞書のkeyに使えるものらしい | ||
| - defaultdictの方が分岐が一つ減って良かった | ||
| - keyをtupleにする方法もあった | ||
| - 選択肢としてもっておきたい | ||
| - `chrは本来組み込み関数chr()の名前なので、変数名として使うのは避けたほうが良いように思います。ch or c でいかがでしょうか?` | ||
| - 関数があることを知らなかった。組み込み関数は数がそんなに多くなさそうなので全部目を通しておく | ||
| - https://docs.python.org/ja/3/library/functions.html | ||
| - https://github.com/naoto-iwase/leetcode/pull/12/files | ||
| - `NUM_CHARACTERS = len(string.ascii_lowercase)`の書き方はとても良かったので使う時は思い出したい | ||
| - 予約後はできるだけ変数名にしない(https://github.com/naoto-iwase/leetcode/pull/12/files#r2415311731) | ||
| - なのでstrもやめる | ||
|
|
||
| ``` | ||
| FORBIDDEN = { | ||
| # 他言語(C/C++/Java/TS/Go/Python)での予約語になりがち | ||
| "char","int","long","short","float","double","bool","string", | ||
| "class","struct","union","enum","interface","implements","extends", | ||
| "public","private","protected","package","import","export","module", | ||
| "new","delete","this","super","const","var","let","yield","lambda", | ||
| "async","await","from","in","is","not","or","and","record","match","type", | ||
| # Python 組み込み | ||
| "list","dict","set","tuple","id","sum","max","min","any","all","input", | ||
| "bytes","map","filter","range","str","type" | ||
| } | ||
| ``` | ||
| ## step3 | ||
| - `sorted_to_word`はvalueがリストなのでwordsとする(どこかの指摘コメントであったのを書いてる途中で思い出した) | ||
| - 実装は短いのとロジックも単純なのですんなり書けた | ||
|
|
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,21 @@ | ||
| # | ||
| # @lc app=leetcode id=49 lang=python3 | ||
| # | ||
| # [49] Group Anagrams | ||
| # | ||
|
|
||
| # @lc code=start | ||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_to_str = {} | ||
| for str in strs: | ||
| sorted_str = "".join(sorted(str)) | ||
| if sorted_str in sorted_to_str: | ||
| sorted_to_str[sorted_str].append(str) | ||
| else: | ||
| sorted_to_str[sorted_str] = [str] | ||
|
|
||
| return [value for value in sorted_to_str.values()] | ||
|
|
||
|
|
||
| # @lc code=end |
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,21 @@ | ||
| # | ||
| # @lc app=leetcode id=49 lang=python3 | ||
| # | ||
| # [49] Group Anagrams | ||
| # | ||
|
|
||
| # @lc code=start | ||
| import collections | ||
|
|
||
|
|
||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_to_word = collections.defaultdict(list) | ||
| for word in strs: | ||
| sorted_word = "".join(sorted(word)) | ||
| sorted_to_word[sorted_word].append(word) | ||
|
|
||
| return [sorted_to_word.values()] | ||
|
|
||
|
|
||
| # @lc code=end | ||
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,22 @@ | ||
| # | ||
| # @lc app=leetcode id=49 lang=python3 | ||
| # | ||
| # [49] Group Anagrams | ||
| # | ||
|
|
||
| # @lc code=start | ||
| import collections | ||
|
|
||
|
|
||
| class Solution: | ||
| def groupAnagrams(self, strs: List[str]) -> List[List[str]]: | ||
| sorted_to_words = collections.defaultdict(list) | ||
|
|
||
| for word in strs: | ||
| sorted_word = "".join(sorted(word)) | ||
| sorted_to_words[sorted_word].append(word) | ||
|
|
||
| return list(sorted_to_words.values()) | ||
|
|
||
|
|
||
| # @lc code=end |
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.
正しく動かないコードはそう明示しておいたほうがレビュワーに負担がかからないと思います。
また、returnするのが[sorted_to_word.values()]やsorted_to_word.values()だと誤りで、[value for value in sorted_to_word.values()]やlist(sorted_to_word.values())だと正しい理由をdocsを参照して解明し、説明できるようにしておくと良いと思いました。
https://docs.python.org/ja/3/library/stdtypes.html#dict.values