This file contains hidden or 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
// Using deque | |
void max_slid_window(vector<int> v){ | |
deque<int> q; | |
for(int i=0; i<k; i++){ | |
while(!q.empty() && v[i] >= v[q.back()]) | |
q.pop_back(); | |
q.push_back(i); | |
} | |
for(int i=k+1; i<n; i++){ |
This file contains hidden or 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
// Use parititon method of quick sort | |
void quick_sort(vector<int> v){ | |
int i=-1; | |
for(int j=0; j<n; j++){ | |
if(a[j] < 0){ | |
i++; | |
swap(a[i],a[j]); | |
} | |
} |
This file contains hidden or 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
// diameter of binary tree | |
// consider a root --> diameter of a tree = max(if path goes through that root, if path does not go through that root) | |
// so diamter = max(lh+rh+1, max(ld,rd)); | |
int diameter(node *t){ | |
if(t != NULL){ | |
int lh = height(t -> left); | |
int rh = height(t -> right); | |
int ld = diameter(t -> left); |
This file contains hidden or 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
// Converting bst to sorted DLL | |
// go in in order and and keep track of previous node and make it previous to current node and current node next to this node. | |
void bsttodll(node *root, node **head){ | |
if(root == NULL) return ; | |
static node *prev = NULL; | |
bsttodll(root->left, head); | |
if(prev == NULL) *head = root; | |
else { | |
root -> previous = prev; | |
prev -> next = previous; |
This file contains hidden or 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
// http://www.geeksforgeeks.org/next-greater-element/ | |
// simple stack logic. | |
int nge(int a[]){ | |
vector<int> nge; | |
stack<int> stk; | |
stk.push(a[0]); | |
for(int i=1; i<n; i++){ | |
int next = a[i]; | |
while( !stk.empty()){ |
This file contains hidden or 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
/* | |
It's a modification of merge sort | |
see for explanation:- http://www.geeksforgeeks.org/counting-inversions/ | |
*/ | |
int merge_sort(int a[], int low, int high){ | |
if(high > low){ | |
int mid = (low + high)/2; | |
inv_count = merge_sort(a, low, mid); | |
inv_count += merge_sort(a, mid+1, high); | |
return merge(a, temp, low, mid, high); |
This file contains hidden or 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
// As though it looks like O(n^3) but it actally is O(n^2). because of k initialized outisde and does not depenv on j value. | |
int possible_triangles(vector<int> a){ | |
int count = 0; | |
for(int i=0; i<n-2; i++){ | |
int k = i+2; | |
for(int j=i+1; j<n; j++){ | |
while(k < n && a[i] + a[j] > a[k]) ++k; | |
count = count + (k-j-1); | |
} |
This file contains hidden or 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
http://ideone.com/eF7Nih |
This file contains hidden or 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
//http://www.geeksforgeeks.org/pancake-sorting/ | |
void flip(vector<int> v, int i){ | |
int temp, start = 0; | |
while(start < i){ | |
int temp = v[start]; | |
v[start] = v[i]; | |
v[i] = temp; | |
start++; | |
i--; |
This file contains hidden or 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
// Very good problem. But difficult to understand. | |
// http://leetcode.com/2010/11/finding-minimum-window-in-s-which.html | |
int search(vector<string> txt, vector<string> pat, string s1, string s2){ | |
for(int i=0; i<s2.length(); i++) pat[(int)(s2[i])]++; | |
txt[256] = {0}; | |
int begin = 0, end = 0; | |
while(end < s1.length()){ | |
if(pat[s1[end]] == 0) continue; | |
txt[s1[end]]++l; |