From 0dcc02ddf7799fb8775abb0208395b946de73f95 Mon Sep 17 00:00:00 2001 From: Jaswant Date: Sun, 20 Mar 2022 09:10:50 +0530 Subject: [PATCH 1/2] cleanup code --- Algorithms/BFS_GRID.java | 2 -- Algorithms/SuffixArray,HashingSeive.java | 3 +-- Solution.java | 29 +++++++++--------------- 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/Algorithms/BFS_GRID.java b/Algorithms/BFS_GRID.java index 92dbfea..aa3f281 100755 --- a/Algorithms/BFS_GRID.java +++ b/Algorithms/BFS_GRID.java @@ -10,8 +10,6 @@ * Platform : N/A * */ - -/* The Main Class */ public class A { private InputStream inputStream ; diff --git a/Algorithms/SuffixArray,HashingSeive.java b/Algorithms/SuffixArray,HashingSeive.java index 9643757..3e0ffc2 100755 --- a/Algorithms/SuffixArray,HashingSeive.java +++ b/Algorithms/SuffixArray,HashingSeive.java @@ -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. diff --git a/Solution.java b/Solution.java index a6d0b79..59c510a 100644 --- a/Solution.java +++ b/Solution.java @@ -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){ From f577aab6f7ec73a6561e85ced2f7473178ee9256 Mon Sep 17 00:00:00 2001 From: Jaswant Date: Sun, 20 Mar 2022 09:11:51 +0530 Subject: [PATCH 2/2] add binary lifting to answer queries on tree in log N --- Algorithms/BinaryLifting.java | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 Algorithms/BinaryLifting.java diff --git a/Algorithms/BinaryLifting.java b/Algorithms/BinaryLifting.java new file mode 100644 index 0000000..b10844c --- /dev/null +++ b/Algorithms/BinaryLifting.java @@ -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 unVisited = new LinkedList(); // 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; + } +} +