-
Notifications
You must be signed in to change notification settings - Fork 0
Solved: 779. K-th Symbol in Grammar #46
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
Fuminiton
wants to merge
1
commit into
main
Choose a base branch
from
solve-problem46
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,85 @@ | ||
| ## 取り組み方 | ||
| - step1: 5分以内に空で書いてAcceptedされるまで解く + テストケースと関連する知識を連想してみる | ||
| - step2: 他の方の記録を読んで連想すべき知識や実装を把握した上で、前提を置いた状態で最適な手法を選択し実装する | ||
| - step3: 10分以内に1回もエラーを出さずに3回連続で解く | ||
|
|
||
| ## step1 | ||
| 0 | ||
| 0 1 | ||
| 01 10 | ||
| 0110 1001 | ||
| 01101001 10010110 | ||
|
|
||
| 上記のように、前半の反転が後半になることを利用して、1個になるまで範囲を狭めていく。この時、kの位置は、前半にあればそのままで、後半にあればk-全体の半分のサイズにあたる位置に割り当てて次のループに渡す。 | ||
|
|
||
| n回再帰するので、O(n) | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| def caluclate_kth_symbol(n: int, k: int) -> int: | ||
| if n == 1: | ||
| return 0 | ||
| half_rows = 2 ** (n - 2) | ||
| if k <= half_rows: | ||
| return caluclate_kth_symbol(n - 1, k) | ||
| else: | ||
| return 1 - caluclate_kth_symbol(n - 1, k - half_rows) | ||
|
|
||
| if n < 1: | ||
| raise ValueError("n must be a positive integer.") | ||
| if k < 1: | ||
| raise ValueError("k must be a positive integer.") | ||
| return caluclate_kth_symbol(n, k) | ||
| ``` | ||
|
|
||
| ## step2 | ||
| ### 読んだ | ||
| https://github.com/Yoshiki-Iwasa/Arai60/pull/39/files | ||
| https://github.com/hayashi-ay/leetcode/pull/46/files | ||
| https://github.com/olsen-blue/Arai60/pull/47 | ||
| https://github.com/fuga-98/arai60/pull/46/files | ||
|
|
||
| ### 感想 | ||
| - ループでもかけるが、個人的には再帰の方が規則性に沿って扱う範囲を狭めていく部分が伝わりやすく綺麗だと思う。 | ||
| - 2**(n-2)の部分を1<<(n-2)と書く人が多かった。シフト演算の方が早かった。 | ||
| - ビットの反転も、1 - x ではなく、1^x 等でも表現できる。 | ||
|
|
||
| > >>> def lshift_func(n): | ||
| > >>> ... return 1 << n | ||
| > >>> def power_func(n): | ||
| > >>> ... return 2 ** n | ||
|
|
||
| > >>> n = 100 | ||
| > >>> print(f"lshift: {timeit.timeit(lambda: lshift_func(n), number=1000000):.6f}秒) | ||
| > >>> lshift: 0.149755秒 | ||
| > >>> print(f"power: {timeit.timeit(lambda: power_func(n), number=1000000):.6f}秒) | ||
| > >>> power: 0.324463秒 | ||
|
|
||
|
|
||
| ```py | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n == 1: | ||
| return 0 | ||
| half_nums = 1 << (n - 2) | ||
| if k <= half_nums: | ||
| return self.kthGrammar(n - 1, k) | ||
| else: | ||
| return 1 ^ self.kthGrammar(n - 1, k - half_nums) | ||
| ``` | ||
|
|
||
| ## step3 | ||
| 2の冪乗はビット演算ではなく**で、反転は1^xの方がわかりやすいと思ったので、最終的に以下のようになった。 | ||
|
|
||
| ```py | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n == 1: | ||
| return 0 | ||
| half_nums = 2 ** (n - 2) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. half_indexとかのほうが意味に合ってそうな気がします。 |
||
| if k <= half_nums: | ||
| return self.kthGrammar(n - 1, k) | ||
| else: | ||
| return 1 ^ self.kthGrammar(n - 1, k - half_nums) | ||
| ``` | ||
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.
こちらの書き方で leftshift した方が ** を使うより速いですね