Skip to content

Commit efe7940

Browse files
add 779
1 parent 9a8ce12 commit efe7940

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2424
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
25+
|779|[K-th Symbol in Grammar](https://leetcode.com/problems/k-th-symbol-in-grammar/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_779.java) | O(logn) | O(1) | |Medium|
2526
|771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_771.java) | O(n) | O(m) | |Easy|
2627
|767|[Reorganize String](https://leetcode.com/problems/reorganize-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_767.java) | O(klogk) k is the number of unique characters in given String| O(k) | |Medium|
2728
|766|[Toeplitz Matrix](https://leetcode.com/problems/toeplitz-matrix/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_766.java) | O(m*n) | O(1) | |Easy|
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
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+
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+
}
58+
}
59+
lists.add(curr);
60+
}
61+
return lists.get(N).get(K - 1);
62+
}
63+
}
64+
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+
}
71+
72+
}
73+
74+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._779;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _779Test {
10+
private static _779.Solution1 solution1;
11+
private static _779.Solution2 solution2;
12+
13+
@BeforeClass
14+
public static void setup() {
15+
solution1 = new _779.Solution1();
16+
solution2 = new _779.Solution2();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
assertEquals(0, solution1.kthGrammar(1, 1));
22+
assertEquals(0, solution2.kthGrammar(1, 1));
23+
}
24+
25+
@Test
26+
public void test2() {
27+
assertEquals(0, solution1.kthGrammar(2, 1));
28+
assertEquals(0, solution2.kthGrammar(2, 1));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertEquals(1, solution1.kthGrammar(2, 2));
34+
assertEquals(1, solution2.kthGrammar(2, 2));
35+
}
36+
37+
@Test
38+
public void test4() {
39+
assertEquals(1, solution1.kthGrammar(4, 5));
40+
assertEquals(1, solution2.kthGrammar(4, 5));
41+
}
42+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy