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
  • 06:31 (UTC -12:00)
View GitHub Profile
@beingmartinbmc
beingmartinbmc / MazeSolver.java
Created January 12, 2019 12:51
Solves a grid(maze) using breadth first search
import java.util.LinkedList;
import java.util.Queue;
class Point{
int x;
int y;
int distance;
Point(int x, int y, int distance){
this.x = x;
@beingmartinbmc
beingmartinbmc / PrintBrackets.java
Created February 19, 2019 16:35
Printing the bracket number of each bracket.
public class PrintBrackets{
private static void printBrackets(String w){
Stack<Integer> stack = new Stack<>();
int count = 1;
for(char c : w.toCharArray()){
if(isOpeningSymbol(c)) {
System.out.print(count+" ");
stack.add(count++);
}
if(isClosingSymbol(c)) {
@beingmartinbmc
beingmartinbmc / Knapsack.java
Created February 23, 2019 11:42
0/1 Knapsack using Memoization
import java.util.HashMap;
public class Knapsack {
private static HashMap<String, Integer> map = new HashMap<>();
private static int ks(int i, int w, int[] weight, int[] profit){
if(i < 0 || w == 0)
return 0;
String key = i + " -> " + w;
if(map.containsKey(key))
return map.get(key);
@beingmartinbmc
beingmartinbmc / CoinChange.py
Created March 12, 2019 15:32
Includes the memoised version of the coin change problem
def coin_change(coins, amount, items, lookup):
key = str(amount) + '->' + str(items)
if key in lookup:
return lookup[key]
else:
if amount == 0:
lookup[key] = 1
return 1
if amount < 0 or items < 0:
lookup[key] = 0
@beingmartinbmc
beingmartinbmc / Jobs Sequencing.py
Created March 12, 2019 17:17
Clean and concise code for the job sequencing problem. Using greedy approach.
def print_jobs(jobs):
slots = [False] * len(jobs)
result = [9] * len(jobs)
jobs.sort(reverse=True, key=lambda x: x[2])
d_max = max(jobs, key=lambda x: x[1])[1]
count = 0
for i in range(len(jobs)):
k = min(d_max, jobs[i][1])
while k >= 1:
if not slots[k]:
@beingmartinbmc
beingmartinbmc / Trie.java
Created March 16, 2019 18:30
Implementation of a Trie that supports insertion, deletion, updation, and querying the count.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
class TrieNode{
private Map<Character, TrieNode> children;
private boolean wordBreak;
private int count;
@beingmartinbmc
beingmartinbmc / DisjointSet.java
Created April 6, 2019 17:54
Disjoint Sets implementation of makeSet, findSet, and union.
import java.util.*;
class Node{
int data;
Node parent;
int rank;
}
public class DisjointSet{
private Map<Integer, Node> map;
@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) {
@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 / 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] = '#';
}