Skip to content

Create word-break.md#23

Open
tshimosake wants to merge 1 commit intomasterfrom
tshimosake-patch-21
Open

Create word-break.md#23
tshimosake wants to merge 1 commit intomasterfrom
tshimosake-patch-21

Conversation

@tshimosake
Copy link
Owner

can_split[right] = True
break

return can_split[len(s)]
Copy link

Choose a reason for hiding this comment

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

can_split[-1]

でも良さそうだと思いました。

```py
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
word_set = set(wordDict)
Copy link

Choose a reason for hiding this comment

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

自分は変数名には型名を入れないことが多いです。理由は、型名を入れても読み手にとって有益な情報にならないことが多いためです。ただ、今回は list 型と set 型の両方が同じスコープに存在しているため、区別のために、入れたほうが良いかもしれないと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

とても勉強になります。ありがとうございます!

can_split[0] = True

for right in range(1, len(s) + 1):
for left in range(right):
Copy link

Choose a reason for hiding this comment

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

left と wordDict の文字列でループを回すという方法もあります。

can_split = [False] * (len(s) + 1)
can_split[0] = True
for left in range(len(s)):
    if not can_split[left]:
        continue
    
    for word in wordDict:
        if s.startswith(word, left):
            can_split[left + len(word)] = True

return can_split[-1]

Copy link
Owner Author

Choose a reason for hiding this comment

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

ああ、こっちのほうが直観的ですね。ありがとうございます!!

@@ -0,0 +1,113 @@
## step1

5分でわからず ChatGPT に答えを聞いた。以下の回答のような方針は頭によぎったが、二重ループで大丈夫なのかとか、実装が大変そうだななどの不安から手が動かなかった。
Copy link

Choose a reason for hiding this comment

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

このあたり、コストパフォーマンスに見合わないことをしているのではないか、という気持ちを押さえる能力は、たぶんあるんですよね。

日常においても、投機的に実行したり、探索をしたりすることはあるわけです。買い物に行って、「うーん、使わないかもしれないけれどもどうしても必要になる可能性がある。また買いに来る手間を考えると安いから買ってしまおう。」であったり、「おそらくここらへんに売っているから探しに行こう」であったりです。

こういう手段は日常的に取っているはずで、これができないと結構辛いはずです。

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.

3 participants