We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My Java 8 solution, which passes all test cases, for Max Score of 40:
I iterate over the entire tree structure as outlined in List<List<Integer>> indexes, and then I make a Map<Integer, Integer> nodeDepthMap, which maps a node to its corresponding depth in the tree. I also make a Map<Integer, List<Integer>> depthNodesLstMap which maps a given depth as Key to a List of Nodes occurring at that Depth as Value. These then come in handy for the next step, when I iterate through all the Queries, and execute them sequentially, performing swaps, and doing Recursive Inorder Traversals, accumulating the intermediate results.
(Ignore the verbose debug print statements, as those helped in validating my logic with the initial limited test cases, and I commented it out once I verified that the logic is sound).
classResult{publicstaticList<Integer>inorderTraversal(List<List<Integer>>indexes){List<Integer>result=newArrayList<>();performInorderTraversal(indexes,1,result);returnresult;}privatestaticvoidperformInorderTraversal(List<List<Integer>>indexes,intnodeNum,List<Integer>result){if(nodeNum==-1||nodeNum>indexes.size()){return;}performInorderTraversal(indexes,indexes.get(nodeNum-1).get(0),result);// Visit left subtreeresult.add(nodeNum);// Visit current nodeperformInorderTraversal(indexes,indexes.get(nodeNum-1).get(1),result);// Visit left subtree}publicstaticvoidperformSwapOnNode(List<List<Integer>>indexes,intnodeNum){intnodeIdx=(nodeNum-1);inttemp=indexes.get(nodeIdx).get(0);indexes.get(nodeIdx).set(0,indexes.get(nodeIdx).get(1));indexes.get(nodeIdx).set(1,temp);}publicstaticList<List<Integer>>executeAllSwapQueries(List<List<Integer>>indexes,List<Integer>queries,LinkedHashMap<Integer,List<Integer>>depthNodesLstMap){intnumQueries=queries.size();intmaxDepth=depthNodesLstMap.keySet().parallelStream().mapToInt(Integer::intValue).max().orElseThrow(NoSuchElementException::new);List<List<Integer>>postSwapInorderResult=newArrayList<>();System.err.println("");System.err.println("");for(intqIdx=0;qIdx<numQueries;qIdx++){intbaseDepth=queries.get(qIdx);// System.err.println("Processing Query qIdx = >" + qIdx + "< with " + // "baseDepth = >" + baseDepth + "<");for(intcurDepth=baseDepth,curFactor=1;curDepth<=maxDepth;curFactor++,curDepth=(curFactor*baseDepth)){List<Integer>nodesToBeSwappedLst=depthNodesLstMap.get(curDepth);// System.err.println("curDepth = >" + curDepth + "< | " + // "nodesToBeSwappedLst = >" + nodesToBeSwappedLst + "<");for(IntegernodeNum:nodesToBeSwappedLst){performSwapOnNode(indexes,nodeNum);}}List<Integer>curPostSwpInorderLst=inorderTraversal(indexes);postSwapInorderResult.add(qIdx,curPostSwpInorderLst);// System.err.println("Processed Query qIdx = >" + qIdx + "< with " + // "baseDepth = >" + baseDepth + "< | " + // "curPostSwpInorderLst = >" + curPostSwpInorderLst + "<");}// System.err.println("Processed All >" + numQueries + "< Queries. " +// "postSwapInorderResult = >" + postSwapInorderResult + "<");returnpostSwapInorderResult;}/* * Complete the 'swapNodes' function below. * * The function is expected to return a 2D_INTEGER_ARRAY. * The function accepts following parameters: * 1. 2D_INTEGER_ARRAY indexes * 2. INTEGER_ARRAY queries */publicstaticList<List<Integer>>swapNodes(List<List<Integer>>indexes,List<Integer>queries){// Write your code here// System.err.println(""); System.err.println("");// System.err.println("ENTRY: indexes.size() = >" + indexes.size() + "<");// System.err.println("ENTRY: queries.size() = >" + queries.size() + "<");// System.err.println(""); System.err.println("");// System.err.println("ENTRY: indexes = >" + indexes + "<");// System.err.println("ENTRY: queries = >" + queries + "<");intnumNodes=indexes.size();LinkedHashMap<Integer,List<Integer>>depthNodesLstMap=newLinkedHashMap<>();LinkedHashMap<Integer,Integer>nodeDepthMap=newLinkedHashMap<>();IntStream.range(1,numNodes).forEach(i->nodeDepthMap.put(i,0));// Initializing for the Root Node '1', at Depth / Level 1nodeDepthMap.put(1,1);if(!depthNodesLstMap.containsKey(1)){depthNodesLstMap.put(1,newArrayList<Integer>());}depthNodesLstMap.get(1).add(1);for(inti=0;i<indexes.size();i++){intleftChild=indexes.get(i).get(0);intrightChild=indexes.get(i).get(1);intnodeNum=(i+1);if(leftChild!=-1){intleftChildDepth=(nodeDepthMap.get(nodeNum)+1);nodeDepthMap.put(leftChild,leftChildDepth);if(!depthNodesLstMap.containsKey(leftChildDepth)){depthNodesLstMap.put(leftChildDepth,newArrayList<Integer>());}depthNodesLstMap.get(leftChildDepth).add(leftChild);}if(rightChild!=-1){intrightChildDepth=(nodeDepthMap.get(nodeNum)+1);nodeDepthMap.put(rightChild,rightChildDepth);if(!depthNodesLstMap.containsKey(rightChildDepth)){depthNodesLstMap.put(rightChildDepth,newArrayList<Integer>());}depthNodesLstMap.get(rightChildDepth).add(rightChild);}}// System.err.println(""); System.err.println("");// System.err.println("INIT: nodeDepthMap = >" + nodeDepthMap + "<");// System.err.println("INIT: depthNodesLstMap = >" + depthNodesLstMap + "<");returnexecuteAllSwapQueries(indexes,queries,depthNodesLstMap);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Swap Nodes [Algo]
You are viewing a single comment's thread. Return to all comments →
My Java 8 solution, which passes all test cases, for Max Score of 40:
I iterate over the entire tree structure as outlined in
List<List<Integer>> indexes
, and then I make aMap<Integer, Integer> nodeDepthMap
, which maps a node to its corresponding depth in the tree. I also make aMap<Integer, List<Integer>> depthNodesLstMap
which maps a givendepth
as Key to a List of Nodes occurring at that Depth as Value. These then come in handy for the next step, when I iterate through all the Queries, and execute them sequentially, performing swaps, and doing Recursive Inorder Traversals, accumulating the intermediate results.(Ignore the verbose debug print statements, as those helped in validating my logic with the initial limited test cases, and I commented it out once I verified that the logic is sound).