-
Notifications
You must be signed in to change notification settings - Fork 0
Create 779. K-th Symbol in Grammar.md #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
tokuhirat
wants to merge
1
commit into
main
Choose a base branch
from
779.-K-th-Symbol-in-Grammar
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
97 changes: 97 additions & 0 deletions
97
779. K-th Symbol in Grammar/779. K-th Symbol in Grammar.md
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,97 @@ | ||
| # 779. K-th Symbol in Grammar | ||
| ## STEP1 | ||
| - 何も見ずに解いてみる | ||
| - 行数を一つずつ減らしていく。ある行において、文字列の前半にいたら index をそのままに行数を -1 して良い。文字列の後半にいたら、index から文字列の半分の長さを引き、0と1を反転させた回数を +1 し行数を -1 する。 | ||
| - 他の問題より不正な入力が入りやすいと思ったので、チェックしたい。 | ||
| - 書いている段階では明確に整理できていなかったので、読み返すとコードが読みにくい。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| n -= 1 | ||
| k -= 1 | ||
|
|
||
| if n == 0: | ||
| if k != 0: | ||
| raise ValueError("invalid input") | ||
| return 0 | ||
|
|
||
| num_symbols = 2 ** n | ||
| count = 0 | ||
| while num_symbols >= 2: | ||
| if not 0 <= k < num_symbols: | ||
| raise ValueError("invalid input") | ||
| num_symbols //= 2 | ||
| if k >= num_symbols: | ||
| count += 1 | ||
| k -= num_symbols | ||
| if count % 2 == 0: | ||
| return 0 | ||
| else: | ||
| return 1 | ||
| ``` | ||
|
|
||
| ## STEP2 | ||
| ### プルリクやドキュメントを参照 | ||
| - https://github.com/olsen-blue/Arai60/pull/47/files#diff-da439603310f08640b8dab0ec6cfc15251b5669e04e4effc5795dbe1f506a8daR43 | ||
| - k の偶奇での場合分けは気が付かなかった。局所的な関係性で考えるパターン。 | ||
| - k % 2 で場合分けするならば、(k+1) // 2 について k // 2 で良い場合は使い分けたい気がする。下記のように if 文の外に置くのであれば当然 (k+1) // 2 でないといけない。 | ||
|
|
||
| ```python | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n == 0: | ||
| return 0 | ||
|
|
||
| previous_symbol = self.kthGrammar(n - 1, (k + 1) // 2) | ||
| if k % 2 == 0: | ||
| return 1 - previous_symbol | ||
| else: | ||
| return previous_symbol | ||
| ``` | ||
| - https://github.com/olsen-blue/Arai60/pull/47/files#diff-da439603310f08640b8dab0ec6cfc15251b5669e04e4effc5795dbe1f506a8daR65 | ||
| - よくみると n ではなく k に注目しても良いことがわかる | ||
| - https://github.com/fuga-98/arai60/pull/46/files | ||
| - k のビットを考える方法。これは綺麗だけど思いつきにくい。 | ||
| - https://docs.python.org/3/library/stdtypes.html#int.bit_count | ||
| ```python | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| return (k - 1).bit_count() % 2 | ||
| ``` | ||
|
|
||
| - 色々な実装パターンがあるが以下が一番理解しやすいと感じた。 | ||
| ```python | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n < 0: | ||
| raise ValueError("n must be positive.") | ||
| if not 1 <= k <= 2 ** (n - 1): | ||
| raise ValueError("k is out of range") | ||
|
|
||
| result = 0 | ||
| while k > 1: | ||
| if k % 2 == 0: | ||
| result = 1 - result | ||
| k = (k + 1) // 2 | ||
| return result | ||
| ``` | ||
| ## STEP3 | ||
| ### 3回ミスなく書く | ||
| ```python | ||
| class Solution: | ||
| def kthGrammar(self, n: int, k: int) -> int: | ||
| if n < 0: | ||
| raise ValueError("n must be positive") | ||
| if not 1<= k <= 2 ** (n - 1): | ||
| raise ValueError("k is out of range") | ||
|
|
||
| result = 0 | ||
| while k > 1: | ||
| if k % 2 == 0: | ||
|
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. これ簡潔でいいですね。 |
||
| result = 1 - result | ||
| k = (k + 1) // 2 | ||
| return result | ||
| ``` | ||
|
|
||
| 3分,2分,2分で3回Accept | ||
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.
return count % 2で良いと思います。