Skip to content

779. K-th Symbol in Grammar#46

Open
hayashi-ay wants to merge 2 commits intomainfrom
hayashi-ay-patch-35
Open

779. K-th Symbol in Grammar#46
hayashi-ay wants to merge 2 commits intomainfrom
hayashi-ay-patch-35

Conversation

@hayashi-ay
Copy link
Copy Markdown
Owner

@nodchip
Copy link
Copy Markdown

nodchip commented Mar 9, 2024

出力は n の値に依存しません。その点を考慮し、もう少し効率の良い回答を考えてみても良いかもしれません。ただ、そこまで踏み込むと、ただの数学パズルのため、踏み込まなくても良いかもしれません。

自分の解答は以下の通りです。もし興味があれば、なぜ以下の解答でよいか考えてみても良いかもしれません。必須ではありません。

class Solution {
public:
    int kthGrammar(int n, int k) {
        return __builtin_popcount(k - 1) & 1;
    }
};

```

回答を見て再帰で解いた。Binary Treeとして考えれる。
(n, k)の親の位置は(n - 1, (k+1) // 2)になる。1-indexedなのに注意。
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

「親」を表す単語が、問題文中に登場しておりません。そのため、「親」という単語を読んだとき、一瞬何のことか分からないかもしれません。「previous row」というフレーズが登場するので、このフレーズを使ったほうが良いと思います。

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ありがとうございます。自分の頭の中のイメージから来た命名だったので、注釈なしで使うと読む人が混乱してしまいますね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants