Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 139_word_break/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## 問題: [139. Word Break](https://leetcode.com/problems/word-break/description/)
24 changes: 24 additions & 0 deletions 139_word_break/step1.md
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)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dpの方が幾分マシな変数名に感じました。

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)$
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

変数の定義がないですね。ただ、s[i:i + len(word)] in wordDictを見ると違うように見えます。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s[i:i + len(word)] == wordで実装できていればO(nmt)になっていましたが、私が行なった実装だとO(nm^2t)になってしまうと思います。
n: sのサイズ
m: wordDictのサイズ
t: wordDict内の単語の最大サイズ


空間計算量: $O(n)$
22 changes: 22 additions & 0 deletions 139_word_break/step2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Step 2

- DPテーブルの変数、良い名付け方が思いつかない
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Step2で他の人のコードを参照しているのか気になりました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

眺める程度しかできていなかったです。
該当dpの変数名に関しては、他の方のを見る時意識していたのですが、しっくり来なかったです。
単純にdpと命名するのが良かったのかもしれません。


```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)$
22 changes: 22 additions & 0 deletions 139_word_break/step3.md
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:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s[i : i + len(word)] == wordで良いと思います。NeetCodeの動画でもそうなっていました。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これはおっしゃる通りです。in wordDictだとwordDictのサイズ分計算量が増えてしまいます。
リファクタの際に見逃していました。

boolean_list[i] = boolean_list[i + len(word)]
if boolean_list[i]:
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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秒