This file contains 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
// Definition for binary tree | |
class TreeNode { | |
int val; | |
TreeNode left; | |
TreeNode right; | |
TreeNode(int x) { | |
val = x; | |
} | |
} |
This file contains 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 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( |
This file contains 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.Arrays; | |
import java.util.Comparator; | |
class Dog{ | |
int size; | |
public Dog(int s){ | |
size = s; | |
} | |
} | |
This file contains 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
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(); |
This file contains 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 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; |
This file contains 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
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) |
This file contains 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
/* 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]; |
This file contains 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
/* 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] */ |
This file contains 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
// 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 |
This file contains 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 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) |