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 majorityElement(vector<int> &num) { | |
int cnt = 0; | |
for(int i=0; i<num.size(); i++){ | |
if(cnt == 0){ | |
maj = num[i]; | |
cnt++; | |
} | |
else if(maj == num[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
#include <iostream> | |
#include <cstring> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
class Fragile2{ | |
public: | |
int isSafe(vector<vector<int> > &M, int row, int col, vector<vector<bool> > &visited){ |
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
#include <iostream> | |
#include <string> | |
#include <cstring> | |
#include <vector> | |
#include <cmath> | |
using namespace std; | |
int n,k; | |
vector<char> str(35); |
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
/* | |
* Find the longest arithematic progression in an array using DP Method | |
* Reference:- geeksforgeeks | |
* Author: Rohith | |
*/ | |
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
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/connect-nodes-at-same-level/ | |
void foo(t){ | |
if(!t) return; | |
it(t -> left != NULL){ | |
if(t->right != NULL){ | |
t -> left -> next = t -> right; | |
} | |
else { | |
if(t->next->left != 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
// 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; |
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
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
// 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
/* | |
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); |