This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.java.practice; | |
/*** Example of Deep Copy and Shallow Copy ***/ | |
public class Copy { | |
public void caller(){ | |
shallowCopy(); | |
System.out.println(); | |
deepCopy(); | |
} | |
private void shallowCopy(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.decode.strings; | |
import java.util.Stack; | |
/* | |
* Given an encoded string A consisting of lowercase English alphabets, square parentheses, and digits. | |
* The encoding rule is X[encoded string], where the encoded_string inside the square brackets is being repeated exactly X times. | |
* Note that X is guaranteed to be a positive integer. Find and return the decoded string. | |
* Note: You may assume that the original data does not contain any digits and that digits are only for those repeat numbers, X. | |
* For example, there won't be input like 3a or 2[4]. | |
* You may assume that the input string is always valid. No extra white spaces, square brackets are well-formed, etc. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.verma.rishabh; | |
public class PermuteString { | |
public void permutationOfStrings(char[] input,char[] result, int[] countArray,int level){ | |
if(level == input.length){ | |
for(char ch : result){ | |
System.out.print(ch); | |
} | |
System.out.println(); | |
return; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.verma.rishabh; | |
/* ==== Boyer-Moore Vote Algorithm (Majority Element) ==== | |
* Basic concept is that you cant have two elements occurring more tha n n/2 times in an array. | |
* So what this algorithm does is try to cancel out each element with a differing element . | |
* This way, if a majority element exists, it will still persist till the end. | |
*/ | |
public class MooreVotingAlgorithm { | |
// Time O(n) | Space O(1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.binary.tree.views; | |
import com.binary.tree.traversal.BuildTree; | |
import com.binary.tree.traversal.TreeNode; | |
import com.binary.tree.traversal.VerticalTraversalNode; | |
import java.util.LinkedList; | |
import java.util.Queue; | |
import java.util.TreeMap; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.binary.tree.traversal; | |
public class BoundaryTraversal { | |
public void printBoundaryBinaryTree(){ | |
// Left Nodes (Except Leaf) + (Leaf Nodes) + Right Nodes (Reverse Order, Except Root) | |
// Catch : Do not print any node twice. | |
BuildTree bt = new BuildTree(); | |
TreeNode root = bt.getTreeType2(); | |
printLeftNodes(root); | |
printLeafNodes(root); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Comparator; | |
import java.util.PriorityQueue; | |
public class MaxMin_PriorityQueue { | |
int[] test = {4,5,3,76,34,87,97,23,64}; | |
public void maxPriorityQueue(){ | |
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(((int) test.length), new Comparator<Integer>() { | |
@Override | |
public int compare(Integer o1, Integer o2) { | |
return o2 - o1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArrayNode { | |
public Integer value; | |
public Integer arrayId; | |
public ArrayNode(Integer value,Integer arrayId){ | |
this.arrayId = arrayId; | |
this.value = value; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.PriorityQueue; | |
public class SortAnAlmostSortedArray { | |
public void sortNearlySortedArray(int[] arr, int k){ | |
PriorityQueue<Integer> minHeap = new PriorityQueue<>(); | |
int arrayLength = arr.length; | |
// Fill the minHeap from 0th Index till K-1 element | |
for(int i=0; i<k && k<arrayLength; i++){ | |
minHeap.add(arr[i]); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuildMaxHeap { | |
MaxHeapify h = new MaxHeapify(); | |
public void build(int[] arr,int len){ | |
// Optimization : Index of parent of last leaf Node. | |
int startIdx = (len/2)-1; | |
for(int i=startIdx; i>=0;i--){ | |
h.heapify(arr,len,i); | |
} | |
System.out.println("Array representation of Max-Heap is:"); | |
for (int i = 0; i < len; ++i) |