Skip to content

Instantly share code, notes, and snippets.

View beingmartinbmc's full-sized avatar
🏠
Working from home

Ankit Sharma beingmartinbmc

🏠
Working from home
  • Games24x7
  • Bengaluru
  • 09:51 (UTC -12:00)
View GitHub Profile
@beingmartinbmc
beingmartinbmc / MinStack.java
Created June 24, 2020 10:44
Stack that gives you minimum in O(1) time
public class MinStack {
private static class Node {
private int data;
private int min;
private Node next;
Node(int data) {
this.data = data;
this.min = data;
this.next = null;
@beingmartinbmc
beingmartinbmc / FileUploader.java
Last active March 14, 2020 07:22
FileUploader for multipart files
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import java.util.Calendar;
@beingmartinbmc
beingmartinbmc / SuffixArray.py
Created September 22, 2019 07:14
O(nlogn) Suffix Array implementation
class SuffixArray:
def __init__(self, s):
self.sa = self.build_sa(s)
self.lcp = self.build_lcp(s, self.sa)
@staticmethod
def count_sort(k, sa, ra):
n = len(sa)
N = n + 256
nsa = [i for i in sa]
@beingmartinbmc
beingmartinbmc / Factors.java
Created June 17, 2019 13:43
Efficient program to print all factors and prime factors of a number.
import java.util.*;
class GFG{
private static List<Integer> getFactors(int n){
List<Integer> list = new ArrayList<>();
list.add(1); list.add(n);
for(int i=2; i*i <= n; i++){
if(n % i == 0) list.add(i);
if(i*i != n) list.add(n / i);
}
@beingmartinbmc
beingmartinbmc / RotateNumber.java
Last active June 17, 2019 13:15
Rotations of a number. An efficient program to print all rotations of a number (left and right)
import java.io.*;
class GFG{
private static int getDigits(int n){
return (int)(Math.log10(n)) + 1;
}
private static int getFirst(int n){
int digits = getDigits(n);
return n / ((int)Math.pow(10, digits-1));
}
@beingmartinbmc
beingmartinbmc / SegmentTree.java
Last active October 15, 2019 05:43
An efficient segment tree based on this explanation: https://www.youtube.com/watch?v=Oq2E2yGadnU&t=619s
public class SegmentTree {
private int[] tree;
private int n;
SegmentTree(int[] a) {
int n = a.length;
tree = new int[n << 1];
construct(a);
}
@beingmartinbmc
beingmartinbmc / KMP.java
Created May 17, 2019 06:10
Easy implementation of KMP algorithm
package strings;
public class KMP {
private static int[] buildLPS(String pattern){
int[] lps = new int[pattern.length()];
int j = 0;
int i = 1;
while(i < pattern.length()){
if(pattern.charAt(j) == pattern.charAt(i)){
@beingmartinbmc
beingmartinbmc / LongestPalindromicSubstring.java
Last active April 27, 2019 15:04
Implementation of Manacher's Algorithm
package string;
public class Manacher {
private static char[] preprocess(String s){
char[] t = new char[s.length() * 2 + 1];
t[0] = '#';
for(int i=0; i<s.length(); i++){
t[2 * i + 1] = s.charAt(i);
t[2 * i + 2] = '#';
}
@beingmartinbmc
beingmartinbmc / BinaryTree.java
Last active April 24, 2019 06:02
Binary Tree interview questions. All views, All traversals, isBST, lca, size, leaf nodes, insertion(level order), largest bst in bt
import java.util.*;
public class BinaryTree{
static class Node{
int data;
Node left, right;
Node(int data){
this.data = data;
left = right = null;
@beingmartinbmc
beingmartinbmc / AVL.java
Last active June 20, 2019 14:06
Implementation of AVL Tree for competitive programming.
package trees;
import java.util.Arrays;
class AVL {
private class Node{
int data, height;
Node left, right;
Node(int data) {