You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/0700-0799/0779.K-th Symbol in Grammar/README_EN.md
+84-4
Original file line number
Diff line number
Diff line change
@@ -40,7 +40,7 @@ tags:
40
40
<pre>
41
41
<strong>Input:</strong> n = 2, k = 1
42
42
<strong>Output:</strong> 0
43
-
<strong>Explanation:</strong>
43
+
<strong>Explanation:</strong>
44
44
row 1: 0
45
45
row 2: <u>0</u>1
46
46
</pre>
@@ -50,7 +50,7 @@ row 2: <u>0</u>1
50
50
<pre>
51
51
<strong>Input:</strong> n = 2, k = 2
52
52
<strong>Output:</strong> 1
53
-
<strong>Explanation:</strong>
53
+
<strong>Explanation:</strong>
54
54
row 1: 0
55
55
row 2: 0<u>1</u>
56
56
</pre>
@@ -69,7 +69,26 @@ row 2: 0<u>1</u>
69
69
70
70
<!-- solution:start -->
71
71
72
-
### Solution 1
72
+
### Solution 1: Recursion
73
+
74
+
Let's first observe the pattern of the first few rows:
75
+
76
+
```
77
+
n = 1: 0
78
+
n = 2: 0 1
79
+
n = 3: 0 1 1 0
80
+
n = 4: 0 1 1 0 1 0 0 1
81
+
n = 5: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
82
+
...
83
+
```
84
+
85
+
We can see that the first half of each row is exactly the same as the previous row, and the second half is the inversion of the previous row. Note that "inversion" here means changing $0$ to $1$ and $1$ to $0$.
86
+
87
+
If $k$ is in the first half, then the $k$-th character is the same as the $k$-th character of the previous row, so we can directly recurse with $kthGrammar(n - 1, k)$.
88
+
89
+
If $k$ is in the second half, then the $k$-th character is the inversion of the $(k - 2^{n - 2})$-th character of the previous row, i.e., $kthGrammar(n - 1, k - 2^{n - 2}) \oplus 1$.
90
+
91
+
The time complexity is $O(n)$, and the space complexity is $O(n)$.
73
92
74
93
<!-- tabs:start -->
75
94
@@ -128,13 +147,57 @@ func kthGrammar(n int, k int) int {
128
147
}
129
148
```
130
149
150
+
#### TypeScript
151
+
152
+
```ts
153
+
function kthGrammar(n:number, k:number):number {
154
+
if (n==1) {
155
+
return0;
156
+
}
157
+
if (k<=1<< (n-2)) {
158
+
returnkthGrammar(n-1, k);
159
+
}
160
+
returnkthGrammar(n-1, k- (1<< (n-2))) ^1;
161
+
}
162
+
```
163
+
131
164
<!-- tabs:end -->
132
165
133
166
<!-- solution:end -->
134
167
135
168
<!-- solution:start -->
136
169
137
-
### Solution 2
170
+
### Solution 2: Bit Manipulation + Brain Teaser
171
+
172
+
In the problem, the index starts from $1$. We will change $k$ to $k-1$, converting the index to start from $0$. In the following discussion, all indices start from $0$.
173
+
174
+
Upon closer observation, the $i$-th character in a row generates two characters at positions $2i$ and $2i+1$ in the next row.
175
+
176
+
```
177
+
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
178
+
```
179
+
180
+
If the $i$-th character is $0$, then the characters generated at positions $2i$ and $2i+1$ are $0$ and $1$, respectively. If the $i$-th character is $1$, the generated characters are $1$ and $0$.
181
+
182
+
```
183
+
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
184
+
^ * *
185
+
```
186
+
187
+
```
188
+
0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0
189
+
^ * *
190
+
```
191
+
192
+
We can see that the character at position $2i$ (even index) is always the same as the character at position $i$, while the character at position $2i+1$ (odd index) is the inversion of the character at position $i$. In other words, characters at odd indices are always the result of one inversion. If the number of inversions is even, the character remains unchanged; if the number of inversions is odd, it is equivalent to one inversion.
193
+
194
+
Therefore, we only need to check whether $k$ is odd. If it is, we accumulate one inversion. Then, we divide $k$ by $2$ and continue to check, accumulating the number of inversions until $k$ becomes $0$.
195
+
196
+
Finally, we determine whether the number of inversions is odd. If it is, the answer is $1$; otherwise, it is $0$.
197
+
198
+
The process of accumulating the number of inversions is essentially equivalent to counting the number of $1$s in the binary representation of $k$.
199
+
200
+
The time complexity is $O(\log k)$, and the space complexity is $O(1)$.
138
201
139
202
<!-- tabs:start -->
140
203
@@ -175,6 +238,23 @@ func kthGrammar(n int, k int) int {
0 commit comments