Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4fc6f8c

Browse files
nextcommit
1 parent e23beae commit 4fc6f8c

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

‎src/revision/KthSymbolGram.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package revision;
2+
3+
// https://leetcode.com/problems/k-th-symbol-in-grammar/
4+
public class KthSymbolGram {
5+
6+
// here logic involves finding the parent in the previous row.
7+
// look this ques. in term of tree node with left child at(2*i+1 odd)
8+
// and right child at (2*i+2 even) .
9+
// now if k is odd it will be left child of prev row node parent and otherwise right child
10+
// if 0 then 01 and if 1 then 10 now find the parent node and then check if 1 or 0.
11+
12+
public static int method(int n, int k) {
13+
14+
if (n == 1 && k == 1) {
15+
return 0;
16+
}
17+
18+
int prevRowParent = method(n - 1, (k + 1) / 2);
19+
if ((k % 2 == 0 && prevRowParent == 0) || (k % 2 == 1 && prevRowParent == 1)) {
20+
return 1;
21+
}
22+
23+
return 0;
24+
25+
}
26+
27+
public static void main(String[] args) {
28+
29+
System.out.println(method(5, 4));
30+
}
31+
32+
}

‎src/revision/Tribonacci.java

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package revision;
2+
3+
import java.util.HashMap;
4+
5+
public class Tribonacci {
6+
7+
public int tribonacci(int n) {
8+
9+
if (n == 0) {
10+
return n;
11+
}
12+
if (n <= 2) {
13+
return 1;
14+
}
15+
int a = 0, b = 1, c = 1, d;
16+
for (int i = 3; i <= n; i++) {
17+
18+
d = a + b + c;
19+
a = b;
20+
b = c;
21+
c = d;
22+
}
23+
24+
return c;
25+
26+
}
27+
28+
private static HashMap<Integer, Integer> result = null;
29+
30+
public int tribonacci2(int n) {
31+
if (result == null) {
32+
result = new HashMap<>();
33+
}
34+
if (n <= 0) {
35+
return 0;
36+
} else if (n == 1) {
37+
return 1;
38+
}
39+
if (result.containsKey(n)) {
40+
return result.get(n);
41+
}
42+
int valu = tribonacci2(n - 1) + tribonacci2(n - 2) + tribonacci2(n - 3);
43+
result.put(n, valu);
44+
return valu;
45+
}
46+
47+
int trib[] = new int[38];
48+
49+
public int tribonacci3(int n) {
50+
if (n == 0)
51+
return 0;
52+
else if (n <= 2)
53+
return 1;
54+
if (trib[n] != 0)
55+
return trib[n];
56+
int val1 = tribonacci3(n - 1);
57+
int val2 = tribonacci3(n - 2);
58+
int val3 = tribonacci3(n - 3);
59+
60+
trib[n - 1] = val1;
61+
trib[n - 2] = val2;
62+
trib[n - 3] = val3;
63+
64+
return val1 + val2 + val3;
65+
66+
}
67+
68+
}
69+
70+
public static void main(String[] args) {
71+
72+
}
73+
74+
}

‎src/revision/incrementquery.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package revision;
2+
3+
import java.util.Scanner;
4+
5+
// given an empty array and given queries {start index,endIndex}
6+
// you have to increment the subarray btw the given start and end
7+
// brute : O(N*queries) but optimised : O(N+K)
8+
9+
10+
public class incrementquery {
11+
12+
public static void main(String[] args) {
13+
14+
int n = 1000;
15+
16+
17+
int arr[] = new int[n];
18+
Scanner sc = new Scanner(System.in);
19+
int q = sc.nextInt();
20+
int helper[] = new int[n];
21+
while (q-- > 0) {
22+
23+
int start = sc.nextInt();
24+
int end = sc.nextInt();
25+
26+
helper[start] = 1;
27+
helper[end + 1] -= 1;
28+
}
29+
arr[0] = helper[0];
30+
for (int i = 1; i < n; i++) {
31+
32+
arr[i] = helper[i - 1] + helper[i];
33+
34+
}
35+
36+
for (int i = 0; i < n; i++) {
37+
System.out.println(arr[i] + " ");
38+
}
39+
}
40+
41+
42+
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /