Skip to content

Instantly share code, notes, and snippets.

View rohith2506's full-sized avatar

Rohith Uppala rohith2506

View GitHub Profile
/*
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);
/*
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);
/*
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 ;
/*
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
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:
/*
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/
*/
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){
/**
* 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:
/*
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');
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: