Skip to content

Instantly share code, notes, and snippets.

View rishabhverma17's full-sized avatar
💭
I may be slow to respond.

Rishabh Verma rishabhverma17

💭
I may be slow to respond.
View GitHub Profile
@rishabhverma17
rishabhverma17 / Copy.java
Created November 21, 2019 17:50
RandomPractice
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(){
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.
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;
@rishabhverma17
rishabhverma17 / MooreVotingAlgorithm.java
Created November 8, 2019 04:55
Moore's Voting Algorithm (Majority Algorithm)
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)
@rishabhverma17
rishabhverma17 / views_BottomView.java
Created November 7, 2019 11:40
Binary Tree Views
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;
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);
@rishabhverma17
rishabhverma17 / MaxMin_PriorityQueue.java
Created October 20, 2019 07:34
Max and Min Priority Queue in Java.
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;
@rishabhverma17
rishabhverma17 / ArrayNode.java
Last active October 20, 2019 05:56
Merge K Sorted Array's
public class ArrayNode {
public Integer value;
public Integer arrayId;
public ArrayNode(Integer value,Integer arrayId){
this.arrayId = arrayId;
this.value = value;
}
}
@rishabhverma17
rishabhverma17 / SortAnAlmostSortedArray.java
Created October 19, 2019 11:04
Given an array of n elements, where each element is at most k away from its target position, devise an algorithm that sorts in O(n log k) time.
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]);
}
@rishabhverma17
rishabhverma17 / BuildMaxHeap.java
Last active October 19, 2019 07:38
Building Max and Min Heap from Array and performing HeapSort.
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)