|
4 | 4 | import java.util.Arrays;
|
5 | 5 | import java.util.List;
|
6 | 6 |
|
7 |
| -/** |
8 |
| - * 779. K-th Symbol in Grammar |
9 |
| - * |
10 |
| - * On the first row, we write a 0. Now in every subsequent row, |
11 |
| - * we look at the previous row and replace each occurrence of 0 with 01, and each occurrence of 1 with 10. |
12 |
| - * Given row N and index K, return the K-th indexed symbol in row N. (The values of K are 1-indexed.) (1 indexed). |
13 |
| -
|
14 |
| - Examples: |
15 |
| - Input: N = 1, K = 1 |
16 |
| - Output: 0 |
17 |
| -
|
18 |
| - Input: N = 2, K = 1 |
19 |
| - Output: 0 |
20 |
| -
|
21 |
| - Input: N = 2, K = 2 |
22 |
| - Output: 1 |
23 |
| -
|
24 |
| - Input: N = 4, K = 5 |
25 |
| - Output: 1 |
26 |
| -
|
27 |
| - Explanation: |
28 |
| - row 1: 0 |
29 |
| - row 2: 01 |
30 |
| - row 3: 0110 |
31 |
| - row 4: 01101001 |
32 |
| -
|
33 |
| - Note: |
34 |
| -
|
35 |
| - N will be an integer in the range [1, 30]. |
36 |
| - K will be an integer in the range [1, 2^(N-1)]. |
37 |
| - */ |
38 |
| - |
39 | 7 | public class _779 {
|
40 |
| - public static class Solution1 { |
41 |
| - /**Time: O(2^n) |
42 |
| - * Space: O(2^n) |
43 |
| - * This will result int TLE.*/ |
44 |
| - public int kthGrammar(int N, int K) { |
45 |
| - List<List<Integer>> lists = new ArrayList<>(); |
46 |
| - lists.add(Arrays.asList(0)); |
47 |
| - for (int i = 1; i <= N; i++) { |
48 |
| - List<Integer> curr = new ArrayList<>(); |
49 |
| - List<Integer> prev = lists.get(i - 1); |
50 |
| - for (int j = 0; j < prev.size(); j++) { |
51 |
| - if (prev.get(j) == 0) { |
52 |
| - curr.add(0); |
53 |
| - curr.add(1); |
54 |
| - } else { |
55 |
| - curr.add(1); |
56 |
| - curr.add(0); |
57 |
| - } |
| 8 | + public static class Solution1 { |
| 9 | + /** |
| 10 | + * Time: O(2^n) |
| 11 | + * Space: O(2^n) |
| 12 | + * This will result int TLE. |
| 13 | + */ |
| 14 | + public int kthGrammar(int N, int K) { |
| 15 | + List<List<Integer>> lists = new ArrayList<>(); |
| 16 | + lists.add(Arrays.asList(0)); |
| 17 | + for (int i = 1; i <= N; i++) { |
| 18 | + List<Integer> curr = new ArrayList<>(); |
| 19 | + List<Integer> prev = lists.get(i - 1); |
| 20 | + for (int j = 0; j < prev.size(); j++) { |
| 21 | + if (prev.get(j) == 0) { |
| 22 | + curr.add(0); |
| 23 | + curr.add(1); |
| 24 | + } else { |
| 25 | + curr.add(1); |
| 26 | + curr.add(0); |
| 27 | + } |
| 28 | + } |
| 29 | + lists.add(curr); |
| 30 | + } |
| 31 | + return lists.get(N).get(K - 1); |
58 | 32 | }
|
59 |
| - lists.add(curr); |
60 |
| - } |
61 |
| - return lists.get(N).get(K - 1); |
62 | 33 | }
|
63 |
| - } |
64 | 34 |
|
65 |
| - public static class Solution2 { |
66 |
| - /**Time: O(logn) |
67 |
| - * Space: O(1)*/ |
68 |
| - public int kthGrammar(int N, int K) { |
69 |
| - return Integer.bitCount(K - 1) % 2; |
70 |
| - } |
| 35 | + public static class Solution2 { |
| 36 | + /** |
| 37 | + * Time: O(logn) |
| 38 | + * Space: O(1) |
| 39 | + */ |
| 40 | + public int kthGrammar(int N, int K) { |
| 41 | + return Integer.bitCount(K - 1) % 2; |
| 42 | + } |
71 | 43 |
|
72 |
| - } |
| 44 | + } |
73 | 45 |
|
74 | 46 | }
|
0 commit comments