-
Notifications
You must be signed in to change notification settings - Fork 0
Graph bfs dfs 127 #19
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
hiroki-horiguchi-dev
wants to merge
1
commit into
main
Choose a base branch
from
graph-bfs-dfs-127
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.
+110
−4
Open
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 |
|---|---|---|
| @@ -1,6 +1,112 @@ | ||
| - 問題: []() | ||
| - コメント集: [](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy) | ||
| - 問題: [127. Word Ladder](https://leetcode.com/problems/word-ladder/description/) | ||
| - コメント集: [127. Word Ladder](https://docs.google.com/document/d/11HV35ADPo9QxJOpJQ24FcZvtvioli770WWdZZDaLOfg/mobilebasic#h.o0jquy48e6cy) | ||
| - [何かをしようとして、そのことを覚えておきながら別のことをしようとすると、覚えておかないといけないので混乱する](https://discord.com/channels/1084280443945353267/1233603535862628432/1290337478527422528) | ||
| - [名前空間](https://discord.com/channels/1084280443945353267/1201211204547383386/1215702126945112094) | ||
| - もう少し別関数に切り出した方がいい気もするし、25行程度に収まっているのでしない気もする | ||
| - [この問題、結構制限が適当に決められているという印象があります。...](https://discord.com/channels/1084280443945353267/1307605446538039337/1335074776338792512) | ||
| - 「どういうものが動かないかが分かったあとに、回避策がいくつかでてくるようにしておく事自体は大切」 --> その通りだと思った | ||
| - | ||
| - 条件 | ||
| - 1 <= beginWord.length <= 10 | ||
| - endWord.length == beginWord.length | ||
| - 1 <= wordList.length <= 5000 | ||
| - wordList[i].length == beginWord.length | ||
| - beginWord, endWord, and wordList[i] consist of lowercase English letters. | ||
| - beginWord != endWord | ||
| - All the words in wordList are unique. | ||
| - 方針1 | ||
| - 時間計算量: | ||
| - 空間計算量: | ||
| - 最短経路問題なのでBFSを使う | ||
| - beginword を一文字ずつアルファベット入れ替えて、合致するものが wordList にあればQueueにつめる | ||
| - 詰めたものを取り出してまた一文字ずつ入れ替える | ||
| - endword に合致したらその時点での深さ+1を返却する | ||
| - 時間計算量: O(N * word.length * 26) | ||
| - 空間計算量: O(N) | ||
|
|
||
| ## BFS | ||
| ```java | ||
| class Solution { | ||
|
|
||
| public int ladderLength(String beginWord, String endWord, List<String> wordList) { | ||
| if (!wordList.contains(endWord)) { | ||
| return 0; | ||
| } | ||
|
|
||
| Queue<MiddleWordAndDepth> queue = new ArrayDeque<>(); | ||
| queue.add(new MiddleWordAndDepth(beginWord, 1)); | ||
|
|
||
| while (!queue.isEmpty()) { | ||
| MiddleWordAndDepth middle = queue.poll(); | ||
| String middleWord = middle.middleWord; | ||
| int depth = middle.depth; | ||
|
|
||
| for (int i = 0; i < middleWord.length(); i++) { | ||
| for (char c = 'a'; c <= 'z'; c++) { | ||
| String newWord = middleWord.substring(0, i) + c + middleWord.substring(i + 1); | ||
|
|
||
| if (newWord.equals(endWord)) { | ||
| return depth + 1; | ||
| } | ||
|
|
||
| if (wordList.contains(newWord)) { | ||
| queue.add(new MiddleWordAndDepth(newWord, depth + 1)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| record MiddleWordAndDepth(String middleWord, int depth) {} | ||
| } | ||
| ``` | ||
|
|
||
| ## BFS 2回目 | ||
| - 訪問済み管理を忘れたので無限ループで落ちた, hot がQueueに積まれ続けて無限ループ | ||
| ```shell | ||
| beginWord = "hot" | ||
| endWord = "dog" | ||
| wordList = ["hot","dog"] | ||
| ``` | ||
|
|
||
| ```java | ||
| class Solution { | ||
|
|
||
| public int ladderLength(String beginWord, String endWord, List<String> wordList) { | ||
| Set<String> wordSet = new HashSet<>(wordList); | ||
|
|
||
| if (!wordSet.contains(endWord)) { | ||
| return 0; | ||
| } | ||
|
|
||
| Queue<MiddleWordAndDepth> queue = new ArrayDeque<>(); | ||
| queue.add(new MiddleWordAndDepth(beginWord, 1)); | ||
| wordSet.remove(beginWord); | ||
|
|
||
| while (!queue.isEmpty()) { | ||
| MiddleWordAndDepth middle = queue.poll(); | ||
| String middleWord = middle.middleWord; | ||
| int depth = middle.depth; | ||
|
|
||
| for (int i = 0; i < middleWord.length(); i++) { | ||
| for (char c = 'a'; c <= 'z'; c++) { | ||
| String newWord = middleWord.substring(0, i) + c + middleWord.substring(i + 1); | ||
|
|
||
| if (newWord.equals(endWord)) { | ||
| return depth + 1; | ||
| } | ||
|
|
||
| if (wordSet.contains(newWord)) { | ||
| wordSet.remove(newWord); | ||
| queue.add(new MiddleWordAndDepth(newWord, depth + 1)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
| record MiddleWordAndDepth(String middleWord, int depth) {} | ||
| } | ||
| ``` | ||
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.
MiddleWordは個人的には見慣れない命名だと感じました。少し考えて、beginWord、endWordとの対比なのだろうと推測はできました。単にWordのほうが迷わないと感じます。
また、木というよりグラフという感じなので、Depthという言葉は似つかわしくないと思いました。問題文に合わせてLengthはどうでしょうか。
まとめると、MiddleWordAndDepthではなく、WordAndLengthとするのはどうでしょうか。
Uh oh!
There was an error while loading. Please reload this page.
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.
おっしゃる通り。。
解いている時はBFSだしDepthでええやろって思っていたんですが、一日おいてみてみるとそうですね。。
うーん、中間の言葉とその長さ、うーん。
仕事しつつ、いい命名探してみます