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
/* | |
Recursive version | |
Reference:- http://www.geeksforgeeks.org/print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n/ | |
*/ | |
class Solution { | |
public: | |
void combinations(vector<int> arr, vector<int> data, vector<vector<int> > &result, int start, int end, int index, int r){ | |
if(index == r){ | |
result.push_back(data); |
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
/* | |
Dynamic programming solution:- 0(n) | |
f(k) = max.product of numbers from 0 to k | |
g(k) = min.product of numbers from 0 to k | |
f(k) = max(f(k-1)*A[k], A[k], g(k-1)*A[k]); | |
g(k) = min(g(k-1)*A[k], A[k], f(k-1)*A[k]); | |
return f(n-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://www.mytechinterviews.com/permutations-of-a-string | |
*/ | |
class Solution { | |
public: | |
void permutate(vector<int> arr, int index, vector<vector<int> > &result ) { | |
if(index == arr.size()) { | |
result.push_back(arr); | |
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
/* | |
Did not understood it well. should have to focus on it once again | |
http://www.quickperm.org/quickperm.php | |
*/ | |
#define N 12 // number of elements to permute. Let N > 2 | |
void QuickPerm(void) { | |
unsigned int a[N], p[N]; | |
register unsigned int i, j, tmp; // Upper Index i; Lower Index 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
class Solution: | |
# @param path, a string | |
# @return a string | |
def simplifyPath(self, path): | |
result = [] | |
pathList = path.split('/') | |
for content in pathList: | |
if content: | |
if content == '..': | |
try: |
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
/* | |
Simple logic:- | |
if open > close: call close one and increase its count | |
if open < n: call open one and increase its count | |
breaking point: when close == n | |
http://www.geeksforgeeks.org/print-all-combinations-of-balanced-parentheses/ | |
*/ |
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 threeSumClosest(vector<int> &num, int target) { | |
int min = 10e5; | |
int result = 0; | |
sort(num.begin(),num.end()); | |
for(int i=0;i<num.size();i++){ | |
int j = i+1; | |
int k = num.size()-1; | |
while(j < k){ |
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
/** | |
* Definition for singly-linked list with a random pointer. | |
* struct RandomListNode { | |
* int label; | |
* RandomListNode *next, *random; | |
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: |
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 simple three loops | |
*/ | |
class Solution { | |
public: | |
long int stoi(string s){ | |
long int num = 0; | |
for(int i=0;i<s.length();i++) | |
num = (num * 10) + (int)(s[i] - '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
class Solution: | |
# @return an integer | |
def lengthOfLongestSubstring(self, s): | |
prev_length = 0 | |
max_len = 0 | |
char_dict = {} | |
for i,c in enumerate(s): | |
if c in char_dict.keys() and (i - char_dict[c] <= prev_length): | |
prev_length = (i - char_dict[c]) | |
else: |