Skip to content

Instantly share code, notes, and snippets.

View rohith2506's full-sized avatar

Rohith Uppala rohith2506

View GitHub Profile
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:
/*
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
/*
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 ;
/*
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);
/*
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);
/*
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:
// 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)
@rohith2506
rohith2506 / 635_3.cpp
Created October 24, 2014 08:02
To find diameter of graph.(longest path between any two vertices)
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];
@rohith2506
rohith2506 / palindrome_substr.cpp
Created April 30, 2013 11:24
Longest Palindromic sub string(DP Approach)
/*
Longest palindromic substring (DP approach)
time complexity :- O(n^2)
space complexity:- O(n^2)
@Author : rohit
*/
#include <iostream>
#include <stdio.h>
#include <cmath>
@rohith2506
rohith2506 / palindrome_3.cpp
Created April 26, 2013 04:32
palindrome minimum cuts
#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){