-
Notifications
You must be signed in to change notification settings - Fork 0
779. K-th Symbol in Grammar #41
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
akmhmgc
wants to merge
1
commit into
main
Choose a base branch
from
779
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.
+88
−0
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,29 @@ | ||
| # step1 何も見ずに解く | ||
| 簡単な例を書いていくと、n行目のk番目の値はn - 1行目の(k + 1) / 2の値によってわかると気づく。 | ||
| kは半分ずつ減るので時間計算量はO(logk)となる。kの最大値は2^(30 - 1)なので1秒以内に間に合う。 | ||
| stackの深さは最大でも30なのでstack overflowは起きない。 | ||
| 空間計算量はO(n) | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @param {Integer} k | ||
| # @return {Integer} | ||
| def kth_grammar(n, k) | ||
| return -1 if n <= 0 || k <= 0 || k > 2 ** (n - 1) # 不正な値は-1を返す | ||
| return 0 if n == 1 && k == 1 | ||
|
|
||
| if kth_grammar(n - 1, (k + 1) / 2).zero? | ||
| if k.even? | ||
| 1 | ||
| else | ||
| 0 | ||
| end | ||
| else | ||
| if k.even? | ||
| 0 | ||
| else | ||
| 1 | ||
| end | ||
| end | ||
| end | ||
| ``` | ||
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,40 @@ | ||
| # step2 他の方の解答を見る | ||
| ## より簡潔に書く | ||
| https://github.com/tokuhirat/LeetCode/pull/46/ | ||
| 0 -> 01 | ||
| 1 -> 10 | ||
| と子が作られるとすると、左の子であれば親から反転しないし、右側の子であれば反転することを利用する。 | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @param {Integer} k | ||
| # @return {Integer} | ||
| def kth_grammar(n, k) | ||
| return -1 if n <= 0 || k <= 0 || k > 2 ** (n - 1) # 不正な値は-1を返す | ||
| return 0 if n == 1 && k == 1 | ||
|
|
||
| result = kth_grammar(n - 1, (k + 1) / 2) | ||
| result^= 1 if k.even? | ||
| result | ||
| end | ||
| ``` | ||
|
|
||
| ## 再帰を使わずにループで書く | ||
| rootに辿りつくまでに何回反転したかを考える | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @param {Integer} k | ||
| # @return {Integer} | ||
| def kth_grammar(n, k) | ||
| return -1 if n <= 0 || k <= 0 || k > 2 ** (n - 1) # 不正な値は-1を返す | ||
|
|
||
| flips = 0 | ||
| col = k | ||
| while col > 1 | ||
| flips^= 1 if col.even? | ||
| col = (col + 1) >> 1 | ||
| end | ||
| flips | ||
| end | ||
| ``` |
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,18 @@ | ||
| # step3 3回続けて10分以内に書いてエラーを出さなければOKとする | ||
|
|
||
| ```ruby | ||
| # @param {Integer} n | ||
| # @param {Integer} k | ||
| # @return {Integer} | ||
| def kth_grammar(n, k) | ||
| return -1 if n < 1 || k < 1 || k > 2 ** (n - 1) # 不正な値 | ||
|
|
||
| flips = 0 | ||
| col = k | ||
| while col > 1 | ||
| flips^= 1 if k.even? | ||
| col = (col + 1) >> 1 | ||
| end | ||
| flips | ||
| end | ||
| ``` |
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 @@ | ||
| ## step4 レビューを受けて解答を修正 |
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.
私が慣れてないからかもしれませんが、ネストされた長い if 式の値が返り値になっているのが初見では少し読みにくかったです。慣れている人は L17を見て返り値になりそうと読むので大丈夫かもしれないとも思いました。
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.
そうですね。色々書き方があり好みの問題だと思います。
個人的には step2 で書かれているような感じが好みです。