139. Word Break#32
Open
hiro111208 wants to merge 1 commit into
Open
Conversation
liquo-rice
reviewed
May 14, 2026
| @@ -0,0 +1,22 @@ | |||
| # Step 2 | |||
|
|
|||
| - DPテーブルの変数、良い名付け方が思いつかない | |||
Owner
Author
There was a problem hiding this comment.
眺める程度しかできていなかったです。
該当dpの変数名に関しては、他の方のを見る時意識していたのですが、しっくり来なかったです。
単純にdpと命名するのが良かったのかもしれません。
| ```python | ||
| class Solution: | ||
| def wordBreak(self, s: str, wordDict: List[str]) -> bool: | ||
| boolean_list = [False] * (len(s) + 1) |
|
|
||
| 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.
s[i : i + len(word)] == wordで良いと思います。NeetCodeの動画でもそうなっていました。
Owner
Author
There was a problem hiding this comment.
これはおっしゃる通りです。in wordDictだとwordDictのサイズ分計算量が増えてしまいます。
リファクタの際に見逃していました。
| 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]: |
| return boolean_list[0] | ||
| ``` | ||
|
|
||
| 時間計算量: $O(nmt)$ |
There was a problem hiding this comment.
変数の定義がないですね。ただ、s[i:i + len(word)] in wordDictを見ると違うように見えます。
Owner
Author
There was a problem hiding this comment.
s[i:i + len(word)] == wordで実装できていればO(nmt)になっていましたが、私が行なった実装だとO(nm^2t)になってしまうと思います。
n: sのサイズ
m: wordDictのサイズ
t: wordDict内の単語の最大サイズ
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This problem: 139. Word Break
Next problem: 322. Coin Change