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 27f5811

Browse files
Merge pull request #9 from joney000/feature/jassi/BinaryLifting
Feature/jassi/binary lifting
2 parents 31992f5 + f577aab commit 27f5811

File tree

4 files changed

+78
-22
lines changed

4 files changed

+78
-22
lines changed

‎Algorithms/BFS_GRID.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
* Platform : N/A
1111
*
1212
*/
13-
14-
/* The Main Class */
1513
public class A {
1614

1715
private InputStream inputStream ;

‎Algorithms/BinaryLifting.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.util.LinkedList;
2+
/**
3+
* Time: O(N log N + Q * log N), each query is answered in log N time. Space: O(N log N)
4+
* Use:
5+
* Your BinaryLifting object will be instantiated and called as such:
6+
* BinaryLifting obj = new BinaryLifting(n, parent);
7+
* int param_1 = obj.getKthAncestor(node,k);
8+
* ref: https://leetcode.com/problems/kth-ancestor-of-a-tree-node/ and https://www.youtube.com/watch?v=oib-XsjFa-M
9+
*/
10+
class BinaryLifting {
11+
// preprocess
12+
// O(N log N)
13+
// precompute the answer for power of 2
14+
private int[][] atLevel; // atLevel[nodeId][level] means what is the predecessor at 2^level higher
15+
private int MAX_LOG = 0;
16+
boolean vis[];
17+
public BinaryLifting(int n, int[] parent) {
18+
MAX_LOG = 0;
19+
vis = new boolean[n];
20+
while(n >= (1 << MAX_LOG)){
21+
MAX_LOG++;
22+
}
23+
atLevel = new int[n][MAX_LOG];
24+
for(int nodeId = 0; nodeId < n; nodeId++){
25+
for(int level = 0; level < MAX_LOG; level++){
26+
atLevel[nodeId][level] = -1;
27+
}
28+
}
29+
for(int nodeId = 1; nodeId <= n - 1; nodeId++){
30+
if(vis[nodeId])continue;
31+
LinkedList<Integer> unVisited = new LinkedList<Integer>(); // linked list as a stack for unvisited node
32+
int currentNode = nodeId;
33+
while(currentNode != -1 && !vis[currentNode]){
34+
unVisited.addLast(currentNode);
35+
currentNode = parent[currentNode];
36+
}
37+
while(!unVisited.isEmpty()){
38+
int topUnvisitedNode = unVisited.removeLast();
39+
atLevel[topUnvisitedNode][0] = parent[topUnvisitedNode];
40+
for(int level = 1; level <= MAX_LOG - 1; level++){
41+
if(atLevel[topUnvisitedNode][level - 1] != -1){
42+
atLevel[topUnvisitedNode][level] = atLevel[atLevel[topUnvisitedNode][level - 1]][level - 1];
43+
}else{
44+
break;
45+
}
46+
}
47+
vis[topUnvisitedNode] = true;
48+
}
49+
}
50+
}
51+
52+
public int getKthAncestor(int node, int k) {
53+
int kthAncestor = node;
54+
for(int level = MAX_LOG - 1; level >= 0; level--){ // at ancestor at 2^level
55+
if((k & (1 << level)) > 0){ // check if ith bit is set
56+
// every numer can be represented by sum of power of 2
57+
kthAncestor = atLevel[kthAncestor][level];
58+
if(kthAncestor == -1){
59+
break;
60+
}
61+
}
62+
}
63+
return kthAncestor;
64+
}
65+
}
66+

‎Algorithms/SuffixArray,HashingSeive.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010
*
1111
*/
1212

13-
/* The Main Class */
1413
class A{
1514

1615
private InputStream inputStream ;
1716
private OutputStream outputStream ;
1817
private FastReader in ;
19-
private PrintWriter out ;
18+
private PrintWriter out ;
2019
/*
2120
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
2221

‎Solution.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,25 @@ public Solution(boolean stdIO)throws FileNotFoundException{
3737
in = new FastReader(inputStream);
3838
out = new PrintWriter(outputStream);
3939
}
40+
4041
final int MAXN = (int)1e3 + 1;
41-
int [][]mat = new int[MAXN][MAXN];
42-
42+
int [][]matrix = new int[MAXN][MAXN];
43+
4344
void run()throws Exception{
4445
int tests = i();
45-
for(int t = 1; t <= tests; t++){
46+
for(int testCase = 1; testCase <= tests; testCase++){
47+
out.write("Case #"+testCase+": ");
4648
int n = i();
47-
String s = s();
49+
// solve cool things here
50+
51+
long ans = 0L;
4852

49-
out.write("Case #"+t+": ");
50-
for(int i = 1; i <= s.length(); i++){
51-
if(s.charAt(i - 1) == 'S')out.write("E");
52-
else out.write("S");
53-
}
54-
out.write("\n");
53+
out.write(""+ans+"\n");
5554
}
5655
}
57-
boolean isContain4(String a){
58-
String key = ""+a;
59-
for(int pos = 1; pos <= key.length(); pos++){
60-
if(key.charAt(pos - 1)=='4')return true;
61-
}
62-
return false;
63-
}
64-
void clear(){
6556

57+
void clear(){
58+
// Todo: implementation of clearing the shared memory for each test case
6659
}
6760

6861
long gcd(long a, long b){

0 commit comments

Comments
(0)

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