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
void maxElements(int arr[], int sz, int cnt) { | |
if(cnt > sz) return; | |
priority_queue<int, vector<int>, std::greater<int> > q; | |
for(int i = 0; i < sz; i++) { | |
q.push(arr[i]); | |
if(q.size() > cnt) q.pop(); | |
} | |
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
void recur_print(tree* root) { | |
if(root == NULL) return; | |
recur_print(root->lft); | |
cout << root->d << " "; | |
recur_print(root->rt); | |
} | |
void iter_print(tree* root) { | |
if(root == NULL) return; |
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
int fairCoinToss() { | |
// returns either 0 for head, 1 for tail with prob 1/2 for each | |
} | |
// Return a value from 1 to n, with probability 1/n for each | |
int random_number(int n) { | |
int limit = 1; | |
int ret = 0; | |
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
vector<vector<int> > subsets(int a, int b, int n) { | |
vector<vector<int> > ret; | |
if(b < a || (b-a+1) < n) return ret; | |
vector<vector<int> > s1 = subsets(a+1, b, n); | |
vector<vector<int> > s2 = subsets(a+1, b, n-1); | |
for(int i = 0; i < s2.size(); i++) | |
if(s2[i].size() == n-1) s2[i].insert(s2[i].begin(), a); |
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
void recur_print(tree* root) { | |
if(root == NULL) return; | |
recur_print(root->lft); | |
cout << root->d << " "; | |
recur_print(root->rt); | |
} | |
void iter_print(tree* root) { | |
if(root == NULL) return; |
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
class Solution { | |
public: | |
int divide(int dividend, int divisor) { | |
int sign = 1; | |
if(dividend < 0) sign *= -1; | |
if(divisor < 0) sign *= -1; | |
return sign*divideHelper(dividend < 0 ? dividend*-1 : dividend, divisor < 0 ? divisor*-1 : divisor); |
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
node* recur_reverse(node* root) { | |
if(root == NULL) return root; | |
if(root->nxt == NULL) return root; | |
node* nd = recur_reverse(root->nxt); | |
node* lnk = root->nxt; | |
lnk->nxt = root; | |
root->nxt = NULL; | |
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
void recur_print(tree* root) { | |
if(root == NULL) return; | |
recur_print(root->lft); | |
cout << root->d << " "; | |
recur_print(root->rt); | |
} | |
void iter_print(tree* root) { | |
if(root == NULL) return; |
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
double power(double d, int n) { | |
if(n == 0) return 1; | |
int sign = n < 0 ? -1 : 1; | |
n *= sign; | |
double a = power(d, n/2); | |
if(n%2 == 0) a *= a; | |
else a *= a*d; |
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
vector<string> dictionary; | |
int editDistance(const string& s1, const string& s2) { | |
int mem[s1.size()+1][s2.size()+1]; | |
for(int i = 0; i <= s2.size(); i++) | |
mem[0][i] = i; | |
for(int j = 0; j <= s1.size(); j++) | |
mem[j][0] = j; |