-
Notifications
You must be signed in to change notification settings - Fork 0
139. Word Break #32
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?
139. Word Break #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ## 問題: [139. Word Break](https://leetcode.com/problems/word-break/description/) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| # Step 1 | ||
|
|
||
| - わからなかったので[NeetCodeの解説](https://www.youtube.com/watch?v=Sx9NNgInc3A)を見た | ||
| - 末尾から見て「ここから先を辞書の語で切れるか」を`boolean_list[i]`に記録するDP | ||
| - 空文字は `True`(`boolean_list[len(s)]`)。各位置 `i` で、辞書の語が `s` の先頭(`i` から)に合えば、その語の直後 `i+len(word)` が `True` なら `i` も `True`。最後に `boolean_list[0]` が答え。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| boolean_list = [False] * (len(s) + 1) | ||
| boolean_list[len(s)] = True | ||
|
|
||
| for i in range(len(s) - 1, -1, -1): | ||
| for word in wordDict: | ||
| if i + len(word) <= len(s) and s[i:i + len(word)] in wordDict: | ||
| boolean_list[i] = boolean_list[i + len(word)] | ||
| if boolean_list[i]: | ||
| break | ||
| return boolean_list[0] | ||
| ``` | ||
|
|
||
| 時間計算量: $O(nmt)$ | ||
|
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. 変数の定義がないですね。ただ、
Owner
Author
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.
|
||
|
|
||
| 空間計算量: $O(n)$ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Step 2 | ||
|
|
||
| - DPテーブルの変数、良い名付け方が思いつかない | ||
|
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. Step2で他の人のコードを参照しているのか気になりました。
Owner
Author
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. 眺める程度しかできていなかったです。 |
||
|
|
||
| ```python | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| boolean_list = [False] * (len(s) + 1) | ||
| boolean_list[len(s)] = True | ||
|
|
||
| for i in range(len(s) - 1, -1, -1): | ||
| for word in wordDict: | ||
| if i + len(word) <= len(s) and s[i:i + len(word)] in wordDict: | ||
| boolean_list[i] = boolean_list[i + len(word)] | ||
| if boolean_list[i]: | ||
| break | ||
| return boolean_list[0] | ||
| ``` | ||
|
|
||
| 時間計算量: $O(nmt)$ | ||
|
|
||
| 空間計算量: $O(n)$ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Step 3 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| boolean_list = [False] * (len(s) + 1) | ||
| boolean_list[len(s)] = True | ||
|
|
||
| for i in range(len(s) - 1, -1, -1): | ||
| for word in wordDict: | ||
| if i + len(word) <= len(s) and s[i:i + len(word)] in wordDict: | ||
|
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.
Owner
Author
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. これはおっしゃる通りです。 |
||
| boolean_list[i] = boolean_list[i + len(word)] | ||
| if boolean_list[i]: | ||
|
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. 上のif文に入るとき以外は確認不要だと思います。 |
||
| break | ||
| return boolean_list[0] | ||
| ``` | ||
|
|
||
| 1回目: 2分 18秒 | ||
|
|
||
| 2回目: 2分 9秒 | ||
|
|
||
| 3回目: 1分 54秒 | ||
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.
dpの方が幾分マシな変数名に感じました。