Skip to content

Commit c37fecb

Browse files
Create kth_grammar.py
1 parent 85525d8 commit c37fecb

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

kth_grammar.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def kthGrammar(self, n: int, k: int) -> int:
3+
# pattern -
4+
# for nth row
5+
# + mutate the (n-1)th row
6+
# + negate the (n-1)th row
7+
8+
if n == 1 or k == 1:
9+
return 0
10+
11+
mid = pow(2, n - 1) // 2
12+
13+
if k <= mid:
14+
return self.kthGrammar(n - 1, k)
15+
else:
16+
return (int) (not self.kthGrammar(n - 1, k - mid))

0 commit comments

Comments
 (0)