Skip to content

Conversation

@TORUS0818
Copy link
Owner

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
start = 0
stop = 1

Choose a reason for hiding this comment

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

stop より last の方が実態を表していてより適切と書籍(リーダブルコード)で読んだことがあります。

Comment on lines +93 to +94
for left in range(s_len):
for right in range(left, s_len):

Choose a reason for hiding this comment

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

自分なら文字ではなくインデックスであることを強調するために left_iright_i としますね。

Copy link
Owner Author

Choose a reason for hiding this comment

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

#35 (comment)
このようなコメントをいただいた事があり、以降消すようにしています。

Choose a reason for hiding this comment

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

ハンガリアン記法的な話ですね

return max_len
```
思考ログ:
- ややテクニカルだが効率はいいなと感じた

Choose a reason for hiding this comment

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

文字とインデックスの辞書を作成してそれを参照する方法なので、個人的にはそこまでテクニカルではなくオーソドックスな方法だと思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

自分には、最後に見たインデックス情報を活用するこの方法がすぐ出てきませんでした。。
現在のleftより前に戻らないようにする部分も初手でハンドリングできていたか怪しいです。

精進致します。

substring_chars = set()
longest_substring_len = 0
while stop <= len(s):
c = s[stop - 1]

Choose a reason for hiding this comment

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

好みの範疇かもしれませんが、stop (last) のほうが、それを含まないインデックスであるというのは少し違和感があります。
二分探索だと右端を含むか含まないかというのはむしろ自由に書き換えられたほうがよいとは思うのですが、ここは文字列のそのインデックスにアクセスしているので、s[stop] になるように書くのが自然な印象です (そうすると長さを求める部分は stop - start + 1 になりますね)。

Copy link

Choose a reason for hiding this comment

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

私もこっちのほうが好みです。

Copy link
Owner Author

Choose a reason for hiding this comment

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

こういうコメントを頂いた背景があります。
#38 (comment)

Comment on lines +93 to +94
for left in range(s_len):
for right in range(left, s_len):

Choose a reason for hiding this comment

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

ハンガリアン記法的な話ですね

longest_substring_len = 0
for left in range(s_len):
for right in range(left, s_len):
sub_s = s[left:right + 1]

Choose a reason for hiding this comment

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

ここでリストのコピーを作ることになるので非効率ではありますね

```python
class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
letter_to_count = {c: 0 for c in s}

Choose a reason for hiding this comment

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

defaultdict とか Counter を使いたくなります

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
letter_to_count = {c: 0 for c in s}

Choose a reason for hiding this comment

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

あまり意味のない空行のように思います

Copy link
Owner Author

Choose a reason for hiding this comment

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

意味がないですね。
ありがとうございます。

```
思考ログ:
- ややテクニカルだが効率はいいなと感じた
- leftを更新する際に現在のleftより前に戻らないようにすることを注意しないといけない

Choose a reason for hiding this comment

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

私もハマりました


# Step3

かかった時間:4min

Choose a reason for hiding this comment

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

これ 3 回やって 4 min ですかね?突っ込むほどのこともないですが、もうちょっと早くなる気もしており、いや別に早くすることが目的ではないんですが、なにか迷いがあるのかなあ、と思いました。

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにと思い、解き直してみましたが、2分程かかりました。
割と解きながら他の選択肢について思い出していたりすることがあるので、それで余計な時間を使ったのかもしれません。

Comment on lines +120 to +121
letter_to_count[s[right]] += 1
max_len = max(max_len, right - left + 1)
Copy link

Choose a reason for hiding this comment

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

細かいですが、自分ならこの2つの行逆にします。(for文の最後でrightが進むことに関係する部分は動かしたいため)

Copy link
Owner Author

Choose a reason for hiding this comment

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

確かにそうですね。
忘れそうだと思って先に書いてしまったんですかね。

if s[right] in letter_to_last_index:
left = max(left, letter_to_last_index[s[right]] + 1)

max_len = max(max_len, right - left + 1)
Copy link

Choose a reason for hiding this comment

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

最大の長さであることはは読んでいれば割とすぐわかる情報で、むしろresultの方が何についての最大の長さなのか関数名を見ればわかるので(関数名がはっきりしていたら)、個人的には好みです

Comment on lines +93 to +94
for left in range(s_len):
for right in range(left, s_len):
Copy link

Choose a reason for hiding this comment

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

s_lenと変数を作るより普通にlen(s)のままの方がわかりやすく思います


文字列の長さをNとして
- 時間計算量:O(N)
- 空間計算量:O(N)

Choose a reason for hiding this comment

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

かなり前のPRへのコメントで恐縮ですが、この問題の制約に

s consists of English letters, digits, symbols and spaces.

とあるので、入力の文字列がめちゃめちゃ大きくなったとしても、substringはあるサイズで頭打ちになる -> 空間計算量はO(1)な気がします。

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.

7 participants