Skip to content

Feature/jassi/binary lifting #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Algorithms/BFS_GRID.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
* Platform : N/A
*
*/

/* The Main Class */
public class A {

private InputStream inputStream ;
Expand Down
66 changes: 66 additions & 0 deletions Algorithms/BinaryLifting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.LinkedList;
/**
* Time: O(N log N + Q * log N), each query is answered in log N time. Space: O(N log N)
* Use:
* Your BinaryLifting object will be instantiated and called as such:
* BinaryLifting obj = new BinaryLifting(n, parent);
* int param_1 = obj.getKthAncestor(node,k);
* ref: https://leetcode.com/problems/kth-ancestor-of-a-tree-node/ and https://www.youtube.com/watch?v=oib-XsjFa-M
*/
class BinaryLifting {
// preprocess
// O(N log N)
// precompute the answer for power of 2
private int[][] atLevel; // atLevel[nodeId][level] means what is the predecessor at 2^level higher
private int MAX_LOG = 0;
boolean vis[];
public BinaryLifting(int n, int[] parent) {
MAX_LOG = 0;
vis = new boolean[n];
while(n >= (1 << MAX_LOG)){
MAX_LOG++;
}
atLevel = new int[n][MAX_LOG];
for(int nodeId = 0; nodeId < n; nodeId++){
for(int level = 0; level < MAX_LOG; level++){
atLevel[nodeId][level] = -1;
}
}
for(int nodeId = 1; nodeId <= n - 1; nodeId++){
if(vis[nodeId])continue;
LinkedList<Integer> unVisited = new LinkedList<Integer>(); // linked list as a stack for unvisited node
int currentNode = nodeId;
while(currentNode != -1 && !vis[currentNode]){
unVisited.addLast(currentNode);
currentNode = parent[currentNode];
}
while(!unVisited.isEmpty()){
int topUnvisitedNode = unVisited.removeLast();
atLevel[topUnvisitedNode][0] = parent[topUnvisitedNode];
for(int level = 1; level <= MAX_LOG - 1; level++){
if(atLevel[topUnvisitedNode][level - 1] != -1){
atLevel[topUnvisitedNode][level] = atLevel[atLevel[topUnvisitedNode][level - 1]][level - 1];
}else{
break;
}
}
vis[topUnvisitedNode] = true;
}
}
}

public int getKthAncestor(int node, int k) {
int kthAncestor = node;
for(int level = MAX_LOG - 1; level >= 0; level--){ // at ancestor at 2^level
if((k & (1 << level)) > 0){ // check if ith bit is set
// every numer can be represented by sum of power of 2
kthAncestor = atLevel[kthAncestor][level];
if(kthAncestor == -1){
break;
}
}
}
return kthAncestor;
}
}

3 changes: 1 addition & 2 deletions Algorithms/SuffixArray,HashingSeive.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@
*
*/

/* The Main Class */
class A{

private InputStream inputStream ;
private OutputStream outputStream ;
private FastReader in ;
private PrintWriter out ;
private PrintWriter out ;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.

Expand Down
29 changes: 11 additions & 18 deletions Solution.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,25 @@ public Solution(boolean stdIO)throws FileNotFoundException{
in = new FastReader(inputStream);
out = new PrintWriter(outputStream);
}

final int MAXN = (int)1e3 + 1;
int [][]mat = new int[MAXN][MAXN];
int [][]matrix = new int[MAXN][MAXN];

void run()throws Exception{
int tests = i();
for(int t = 1; t <= tests; t++){
for(int testCase = 1; testCase <= tests; testCase++){
out.write("Case #"+testCase+": ");
int n = i();
String s = s();
// solve cool things here

long ans = 0L;

out.write("Case #"+t+": ");
for(int i = 1; i <= s.length(); i++){
if(s.charAt(i - 1) == 'S')out.write("E");
else out.write("S");
}
out.write("\n");
out.write(""+ans+"\n");
}
}
boolean isContain4(String a){
String key = ""+a;
for(int pos = 1; pos <= key.length(); pos++){
if(key.charAt(pos - 1)=='4')return true;
}
return false;
}
void clear(){

void clear(){
// Todo: implementation of clearing the shared memory for each test case
}

long gcd(long a, long b){
Expand Down