Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:11243118
Last active October 24, 2015 04:19
IsBST - is binary search tree
// Definition for binary tree
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
@gabhi
gabhi / gist:11243437
Created April 24, 2014 06:16
Levenshtein Edit Distance Algorithm in Java
public static int distance(String s1, String s2){
int edits[][]=new int[s1.length()+1][s2.length()+1];
for(int i=0;i<=s1.length();i++)
edits[i][0]=i;
for(int j=1;j<=s2.length();j++)
edits[0][j]=j;
for(int i=1;i<=s1.length();i++){
for(int j=1;j<=s2.length();j++){
int u=(s1.charAt(i-1)==s2.charAt(j-1)?0:1);
edits[i][j]=Math.min(
@gabhi
gabhi / gist:11243505
Created April 24, 2014 06:20
java custom comparator example
import java.util.Arrays;
import java.util.Comparator;
class Dog{
int size;
public Dog(int s){
size = s;
}
}
@gabhi
gabhi / gist:11243583
Created April 24, 2014 06:24
find min and max in minimum comparisons
class Pair {
int min;
int max;
}
public class Solution {
public static Pair getMinMax(int arr[], int low, int high) {
Pair result = new Pair();
Pair left = new Pair();
@gabhi
gabhi / gist:11243698
Last active August 29, 2015 14:00
length of longest substring without repeating characters
public int lengthOfLongestSubstring(String s) {
Boolean [] exist =new Boolean [256];
for (int i=0; i<256;i++){
exist[i]=false;
}
int i=0,j=0,n=s.length(),maxlen=0;
@gabhi
gabhi / gist:11244538
Created April 24, 2014 07:11
lca lowest common ancestor
struct node *lca(struct node* root, int n1, int n2)
{
if (root == NULL) return NULL;
// If both n1 and n2 are smaller than root, then LCA lies in left
if (root->data > n1 && root->data > n2)
return lca(root->left, n1, n2);
// If both n1 and n2 are greater than root, then LCA lies in right
if (root->data < n1 && root->data < n2)
@gabhi
gabhi / gist:11244780
Created April 24, 2014 07:22
Longest Common Subsequence
/* Dynamic Programming implementation of LCS problem */
#include<stdio.h>
#include<stdlib.h>
int max(int a, int b);
/* Returns length of LCS for X[0..m-1], Y[0..n-1] */
int lcs( char *X, char *Y, int m, int n )
{
int L[m+1][n+1];
@gabhi
gabhi / gist:11244908
Created April 24, 2014 07:28
length of longest common substring
/* Dynamic Programming solution to find length of the longest common substring */
#include<iostream>
#include<string.h>
using namespace std;
// A utility function to find maximum of two integers
int max(int a, int b)
{ return (a > b)? a : b; }
/* Returns length of longest common substring of X[0..m-1] and Y[0..n-1] */
@gabhi
gabhi / gist:11245288
Created April 24, 2014 07:41
length-of-the-longest-arithmatic-progression-in-a-sorted-array
// Returns length of the longest AP subset in a given set
int lenghtOfLongestAP(int set[], int n)
{
if (n <= 2) return n;
// Create a table and initialize all values as 2. The value of
// L[i][j] stores LLAP with set[i] and set[j] as first two
// elements of AP. Only valid entries are the entries where j>i
int L[n][n];
int llap = 2; // Initialize the result
@gabhi
gabhi / gist:11245665
Created April 24, 2014 07:56
Longest increasing subsequence
public class LIS {
public static <E extends Comparable<? super E>> List<E> lis(List<E> n) {
List<Node<E>> pileTops = new ArrayList<Node<E>>();
// sort into piles
for (E x : n) {
Node<E> node = new Node<E>();
node.value = x;
int i = Collections.binarySearch(pileTops, node);
if (i < 0) i = ~i;
if (i != 0)