-
Notifications
You must be signed in to change notification settings - Fork 0
779. K-th Symbol in Grammer #29
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
Open
nittoco
wants to merge
1
commit into
main
Choose a base branch
from
nittoco-patch-29
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| ### Step1 | ||
|
|
||
| - まずは試しに全探索。これはまあダメ、TLEですね。予想通り。 | ||
| - よく考えたら文字列で集計せずに、最初からintにしてもよかった | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| result = '0' | ||
| replaced_chars = [] | ||
| for _ in range(n - 1): | ||
| for c in result: | ||
| if c == '0': | ||
| replaced_chars.append('01') | ||
| else: | ||
| replaced_chars.append('10') | ||
| result = "".join(replaced_chars) | ||
| replaced_chars = [] | ||
| return int(result[k - 1]) | ||
| ``` | ||
|
|
||
| - 2進法ぽい | ||
| - 14番目は前の7番目から発生したもので、その中の2個目なので7番目の反転で〜と考えると | ||
| - 0-indexにすれば13番目→1101、は前の6番目→110(1101の右シフト1回)から出るけどラスト1なのでそれを反転する、みたいに考えれば、2進法のbit分だけ反転する。0番目の時は0なのでbitcountが奇数か判定すればOK | ||
| - 中学受験の算数ぽい(好きではある) | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| return (k - 1).bit_count() % 2 | ||
| ``` | ||
|
|
||
|
|
||
| ## Step2 | ||
|
|
||
| - 再帰という手もあったか、思いつかなかった(というか発想がいきなり2進法にいった) | ||
| - k_prevとか設けずいきなり引数に突っ込んでもよかった | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n == 1: | ||
| return 0 | ||
| is_odd_index = k % 2 | ||
| k_prev = (k + 1) // 2 | ||
| if is_odd_index: | ||
| return self.kthGrammar(n - 1, k_prev) | ||
| else: | ||
| return 1 - self.kthGrammar(n - 1, k_prev) | ||
| ``` | ||
|
|
||
| - bitcountについての資料を読む | ||
|
|
||
| https://stackoverflow.com/questions/109023/count-the-number-of-set-bits-in-a-32-bit-integer | ||
|
|
||
| - x86というプロセッサなら命令がそのままあるらしい | ||
| - それ使わないやつが非常に面白い。(偶数bit + 奇数bit)によって先頭から2桁ずつbitcountして結果を2桁ずつに収める、先頭から4桁ずつbitcountして結果を4桁ずつに収める、を順に行う(説明が下手) | ||
| - [CPythonのコード](https://github.com/python/cpython/blob/main/Include/internal/pycore_bitutils.h#L95)の該当箇所も発見。#if (defined(**clang**) || defined(**GNUC**))であるならbuitinのpopcountを使ってx86のプロセッサの命令を使用。そうでなければ、上記のアルゴリズムでやってる(最後だけ掛け算使ってる?実質同じ操作だけど) | ||
| - Cのlongに収まらない場合は[これ](https://github.com/python/cpython/blob/main/Objects/longobject.c#L6266C1-L6266C19)がCPython実装?多倍長はstructの中の配列にn進法の各桁(nはoverflowしない範囲)が入っているので、単純に各要素で上記の関数を実行して足している | ||
| - 他参考資料 | ||
|
|
||
| https://github.com/hayashi-ay/leetcode/pull/46 | ||
|
|
||
| - [0, 1][1, 0]への辞書として持つ選択肢もあり | ||
| - https://github.com/goto-untrapped/Arai60/pull/26/files | ||
| - https://github.com/Exzrgs/LeetCode/pull/12/files | ||
| - kのbitcountをあえてiterativeに実装(right to leftでやってみた) | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| k -= 1 | ||
| result_digit = 0 | ||
| while k: | ||
| if k & 1: | ||
| result_digit ^= 1 | ||
| k >>= 1 | ||
| return result_digit | ||
| ``` | ||
|
|
||
| - 前半一緒なのと、それに対する反転とみて再帰 | ||
| - これなんでそうなるか考えたけど意外とむずい | ||
|
|
||
| ## Step3 | ||
|
|
||
| ```python | ||
|
|
||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n == 1: | ||
| return 0 | ||
| if k % 2 == 0: | ||
| return 1 - self.kthGrammar(n - 1, (k + 1) // 2) | ||
| else: | ||
| return self.kthGrammar(n - 1, (k + 1) // 2) | ||
| ``` | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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://www.oreilly.co.jp//books/9784873112886/mobile.html
「Binary Hacks ─ ハッカー秘伝のテクニック100選」薦めておきます。
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://www.amazon.co.jp/%E3%83%8F%E3%83%83%E3%82%AB%E3%83%BC%E3%81%AE%E3%81%9F%E3%81%AE%E3%81%97%E3%81%BF%E2%80%95%E6%9C%AC%E7%89%A9%E3%81%AE%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9E%E3%81%AF%E3%81%84%E3%81%8B%E3%81%AB%E3%81%97%E3%81%A6%E5%95%8F%E9%A1%8C%E3%82%92%E8%A7%A3%E3%81%8F%E3%81%8B-%E3%82%B8%E3%83%A5%E3%83%8B%E3%82%A2-%E3%83%98%E3%83%B3%E3%83%AA%E3%83%BC%E3%83%BBS-%E3%82%A6%E3%82%A9%E3%83%BC%E3%83%AC%E3%83%B3/dp/4434046683
「ハッカーのたのしみ: 本物のプログラマはいかにして問題を解くか」もでしょうか…。
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.
Binary Hacksには、ビット演算ネタはほとんどなさそうでしょうか…
新しいのがもうすぐ出るみたいですね
https://www.amazon.co.jp/Binary-Hacks-Rebooted-%E2%80%94%E4%BD%8E%E3%83%AC%E3%82%A4%E3%83%A4%E3%81%AE%E4%B8%96%E7%95%8C%E3%82%92%E6%8E%A2%E6%A4%9C%E3%81%99%E3%82%8B%E3%83%86%E3%82%AF%E3%83%8B%E3%83%83%E3%82%AF89%E9%81%B8-%E6%B2%B3%E7%94%B0/dp/4814400853
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.
どちらも面白そうですね、ありがとうございます