Skip to content

139. Word Break#32

Open
hiro111208 wants to merge 1 commit into
mainfrom
139-word-break
Open

139. Word Break#32
hiro111208 wants to merge 1 commit into
mainfrom
139-word-break

Conversation

@hiro111208
Copy link
Copy Markdown
Owner

This problem: 139. Word Break

Next problem: 322. Coin Change

Comment thread 139_word_break/step2.md
@@ -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と命名するのが良かったのかもしれません。

Comment thread 139_word_break/step1.md
```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の方が幾分マシな変数名に感じました。

Comment thread 139_word_break/step3.md

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のサイズ分計算量が増えてしまいます。
リファクタの際に見逃していました。

Comment thread 139_word_break/step3.md
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]:
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文に入るとき以外は確認不要だと思います。

Comment thread 139_word_break/step1.md
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内の単語の最大サイズ

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants