K-th Symbol in Grammar
Problem statement
Row 1 of a binary grammar is 0. Each later row replaces every 0 with 01 and every 1 with 10.
Given n and one-indexed k, return the k-th symbol in row n.
Function
kthGrammar(n: int, k: int) → intExamples
Example 1
n = 1k = 1return = 0The first row is 0.
Example 2
n = 4k = 5return = 1Row 4 is 01101001 and its fifth symbol is 1.
Constraints
1 <= n <= 30.1 <= k <= 2^(n-1).