-
Notifications
You must be signed in to change notification settings - Fork 0
139. Word Break #34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
139. Word Break #34
Conversation
| 後から文字列のスライスのコストを考慮してなかったことに気づいた。 | ||
| 文字列のスライスコストを入れるとN * W * (L + N)なのでL * N^2になる。そうなるとRubyでギリギリかもしれないが、LeetCode上では想定以上に速い気がする。 | ||
|
|
||
| Rubyのスライスメソッドを見たところ、ASCII文字列のスライスはO(1)であるが、マルチバイト文字列のスライスはO(N)らしい。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
面白いですね。
| # @return {Boolean} | ||
| def word_break(str, word_dict) | ||
| unreachable_suffixes = Set.new | ||
| is_reachable = ->(str){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ruby でどの程度スタイルガイドが参照されているのかわかっていませんが、スタイルガイドでは複数行の場合は lambda を使うのが良いとされていました。
https://github.com/rubocop/ruby-style-guide?tab=readme-ov-file#lambda-multi-line
| word_hashes.add(hash) | ||
| end | ||
|
|
||
| reachable = Array.bnew(str_size, false) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Array.new でしょうか。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
おっしゃる通りです。
コピーする時に誤って入ったのかもしれないです。
|
|
||
| hash = 0 | ||
| (i...str_size).each do |j| | ||
| hash = (hash * base + byte_of.call(str.getbyte(j))) % mod |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
この処理はL53にもあるため、関数にしたら良さそうと思いました。
next_rolling_hash = lambda do |hash, c, base, mod|
(hash * base + byte_of.call(c)) % mod
endc の型は文字列でも良いかもしれません。
https://discord.com/channels/1084280443945353267/1201211204547383386/1235165575592939521
| false | ||
| } | ||
| is_reachable.call(str) | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
自分は再帰で練習しませんでしたが、変数名含めて読みやすかったです。
解いた問題
139. Word Break
使用言語
Ruby
次に解く問題
322. Coin Change