-
Notifications
You must be signed in to change notification settings - Fork 0
779. K-th Symbol in Grammar #48
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?
Conversation
| if k == 1: | ||
| return 0 | ||
|
|
||
| if k % 2: |
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.
if k % 2 == 1 と書いた方がわかりやすいように思いました。
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.
反転する処理をメインにした方が良いという意図でしょうか?
| - https://github.com/olsen-blue/Arai60/pull/47 | ||
| - 2分木を考えてkの偶奇で親の値と同じになるかを判定 | ||
| - https://github.com/olsen-blue/Arai60/pull/47/files#r2002307405 | ||
| - これは思いつかないが知っておこう |
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.
私が想像する出題者の感覚として、なんらかの方法で書いてもらって、そこから誘導をかけながらビットの数であることに気が付いてもらい、連想する知識を見る、くらいかなと思います。
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.
思いつかないようなものでも、考え方を聞いて、(あまり時間をかけずに咀嚼して)選択肢の一つとして引き出しにしまっておくのは大事だと思いました。
色々な選択肢を持って選ぶ感覚を持つ、ということですね。
| - 上記を踏まえると、len(row(t+1)) = 2*len(row(t))に注意して | ||
| - k <= len(row(t))なら、row(t+1)のkは、row(t)のkと同じ | ||
| - k > len(row(t))なら、row(t+1)のkは、row(t)のk-len(row(t))を反転させたものと同じ | ||
| - ところでこの関係性ってtrivialなものなのか?? |
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.
観察すれば思いつきやすいですが、意外と自明ではないですよね
| - https://github.com/yukik8/leetcode/commit/a0e14f3fbe34720d13fcb61916c99d2a424b7bc6 | ||
| - https://github.com/nittoco/leetcode/pull/29 | ||
| - bitcount | ||
| - 初手コレの人もいるのか |
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.
思いつきやすかったのは、昔中学受験の算数で、似たような問題があったのかもしれません
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.
こんなことやるんですね。
私も中学受験しましたが、遊びで参加していたのでガチ勢がどういうことをやるのかあまりよく分かっておらず。
子供がその気になったら面白そうなので自分もやってみようかなと思いました。
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if k == 1: | ||
| return 0 | ||
|
|
||
| if k % 2: | ||
| return self.kthGrammar(n - 1, (k + 1) // 2) | ||
| else: | ||
| return 1 - self.kthGrammar(n - 1, (k + 1) // 2) |
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.
kに1引いて0-indexedから考えて行った方が個人的にわかりやすく感じます。
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.
nittocoさんの解法の感じですよね。
私もその方が自然だと思いまいた
そもそもkを弄る必要がある時点であまりこの解法は想定されていないのかもしれませんね。
| return 0 | ||
|
|
||
| if k <= row_len // 2: | ||
| return self.kthGrammar(n-1, k) |
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.
- の両側にスペースを空けることをお勧めします。
参考までにスタイルガイドへのリンクを貼ります。
https://peps.python.org/pep-0008/#whitespace-in-expressions-and-statements
Always surround these binary operators with a single space on either side: assignment (=), augmented assignment (+=, -= etc.), comparisons (==, <, >, !=, <=, >=, in, not in, is, is not), Booleans (and, or, not).
https://google.github.io/styleguide/pyguide.html#s3.6-whitespace
Surround binary operators with a single space on either side for assignment (=), comparisons (==, <, >, !=, <>, <=, >=, in, not in, is, is not), and Booleans (and, or, not). Use your better judgment for the insertion of spaces around arithmetic operators (+, -, *, /, //, %, **, @).
ただし、上記のスタイルガイドは唯一絶対のルールではなく、複数あるスタイルガイドの一つに過ぎないということを念頭に置くことをお勧めします。
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.
ご指摘ありがとうございます。
意図せずこの書き方になっていました。
最後のコメントも大事ですね。結構原理主義的になってしまいがちなので、チームで合意することを最優先にして進めるように気をつけます。
https://leetcode.com/problems/k-th-symbol-in-grammar/description/