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
/* | |
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
/* | |
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
/* | |
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
/* | |
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
/* | |
Great solution:- | |
Idea is to swap each positive integer you encounter to its "rightful" place at index (x-1) where x is the integer. It's O(n) because you visit each integer in at most 2 unique loop iterations. | |
Best time complexity:- O(n) | |
Worst time complexity:- O(n^2) | |
Space:- O(1) | |
*/ | |
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
// Pre-order using iterative traversal | |
void preorder(node *root){ | |
stack<node *> stk; | |
stk.push(root); | |
while(!stk.empty()){ | |
node *temp = stk.top(); | |
cout << temp -> data << " "; | |
stk.pop(); | |
if(temp -> right) |
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
const long INF = 4000000000000LL; | |
int n; | |
// Storing the graph. Each g[i] is a list of edges adjacent to vertex i | |
// each edge is a pair (j,e), where j is the other vertex connected to i | |
// and e is the id of the edge. So L[e] is the weight of the edge. | |
vector<list<tuple<int, int>>> g; | |
vector<int> L; | |
long dist[2000]; |
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
/* | |
Longest palindromic substring (DP approach) | |
time complexity :- O(n^2) | |
space complexity:- O(n^2) | |
@Author : rohit | |
*/ | |
#include <iostream> | |
#include <stdio.h> | |
#include <cmath> |
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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <cstring> | |
#include <cmath> | |
#define MAXI 1000000 | |
using namespace std; | |
int func(string s){ |