Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

space extra #60

Open
wants to merge 97 commits into
base: add-license-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
97 commits
Select commit Hold shift + click to select a range
8576d80
Merge pull request #3 from royalpranjal/add-license-1
May 9, 2018
23de3d3
Update ConstructBinaryTreeFromInorderAndPreorder.cpp
tarunpratap9754 Jun 19, 2018
2ae7196
Merge pull request #4 from tarunpratap9754/patch-1
Jun 21, 2018
f967f45
largest area of rectangle with permutations added
hardikrana11 Jul 5, 2018
84fea2e
Merge pull request #8 from pockemon/master
Jul 6, 2018
1eacd5f
MinStepsinInfiniteGrid
hardikrana11 Jul 18, 2018
c20c66d
Update MinStepsInInfiniteGrid.cpp
Jul 20, 2018
e10a2bd
Merge pull request #10 from pockemon/master
Jul 20, 2018
b29683a
Scramble string added
hardikrana11 Jul 23, 2018
2c944b8
Merge pull request #11 from pockemon/master
Jul 23, 2018
ec9c172
Trees - Hotel Reviews Solution
santosh3082 Jul 29, 2018
df1825b
Updated BinaryTreeFromInorderAndPostorder.cpp
Jul 31, 2018
5b63458
Merge pull request #13 from santosh3082/master
Aug 10, 2018
a0ed596
Merge pull request #14 from born-confused/master
Aug 10, 2018
fb8c92f
Added solution for simplify-directory-path(stacks)
deep-shekhar Sep 21, 2018
0c2c050
Merge pull request #16 from deep-shekhar/master
Sep 25, 2018
595df8c
Microsoft Amazon
himanshu98 Oct 8, 2018
534bf39
Added knight on a chessboard
Oct 13, 2018
b48d864
Merge pull request #17 from himanshu98/master
Oct 14, 2018
255047f
Merge pull request #18 from me-ydv-5/patch-1
Oct 14, 2018
ade04f1
Facebook, Google
Oct 14, 2018
fc255b5
Merge pull request #19 from me-ydv-5/patch-2
Oct 14, 2018
ed1355a
FibSUM.cpp
CodeLogist Oct 14, 2018
28b4fc0
made as per problem in interview-bit
CodeLogist Oct 14, 2018
327d448
Create WordSearchBoard.cpp
Oct 14, 2018
5aaa117
2 typo error corrected
CodeLogist Oct 14, 2018
e03c8b8
Update 3SumZero.cpp
Oct 15, 2018
d10e9ad
Merge pull request #21 from me-ydv-5/patch-1
Oct 16, 2018
51aac34
Merge pull request #22 from me-ydv-5/patch-2
Oct 16, 2018
f5af9d3
Merge pull request #20 from CodeLogist/patch-1
Oct 18, 2018
1f37237
Commented the previous solution.
Oct 18, 2018
dced939
Update 2-SumBinaryTree.cpp
Oct 18, 2018
14d700b
Added solution with less code.
Oct 25, 2018
8bdc0c5
Added Smallest sequence with given Primes
khannagautam Oct 31, 2018
1f2d1bc
Adding solution for Ways to form Max Heap
khannagautam Oct 31, 2018
cb8b419
Adding solution for Pretty Json
khannagautam Oct 31, 2018
4d43f0b
Added solution for N Queens
khannagautam Oct 31, 2018
5c9623f
Update N Queens
Nov 2, 2018
7a0f272
Merge pull request #30 from khannagautam/patch-3
Nov 2, 2018
af1bbdc
Rename N Queens to NQueens.cpp
Nov 2, 2018
bb6cd65
Rename Pretty Json to PrettyJson.cpp
Nov 2, 2018
108d83b
Merge pull request #29 from khannagautam/patch-2
Nov 2, 2018
3b55176
Rename Smallest sequence with given Primes to SmallestSequenceWithGiv…
Nov 2, 2018
4630434
Merge pull request #26 from khannagautam/khannagautam-patch-1
Nov 2, 2018
540e4f1
Update and rename Ways to form Max Heap to WaysToFormMaxHeap.cpp
Nov 2, 2018
229b984
Merge pull request #28 from khannagautam/patch-1
Nov 2, 2018
86e1376
Tushar'sBirthdayBombs.cpp
khannagautam Nov 3, 2018
244ef26
Merge pull request #31 from khannagautam/patch-4
Nov 3, 2018
447573a
A bit of cleaning.
aryannarora Nov 3, 2018
fbb2188
Merge pull request #32 from aryannarora/patch-1
Nov 12, 2018
1b03fdb
Solutions for problems in math section-added
hardikrana11 Dec 14, 2018
d3facea
Reduce Code size for Anagram.cpp
muditjoshi98 Dec 19, 2018
fc7a2bd
Merge pull request #33 from pockemon/master
Dec 28, 2018
b2c601a
Merge pull request #34 from muditjoshi98/master
Dec 28, 2018
1ca2d40
Update ReverseLinkedList.cpp
Jan 1, 2019
a13c992
Update VerticalOrderTraversalOfBinaryTree.cpp
Mar 6, 2019
91cbf3b
Update PopulateNextRightPointersTree.cpp
Mar 6, 2019
e6f7be7
Update 2-SumBinaryTree.cpp
Mar 6, 2019
12f9d5f
added city Tour in MATH
May 28, 2019
e2a5647
City Tour
me-ydv-5 May 28, 2019
549db93
added SortedPermutatioRank.cpp
May 29, 2019
7291b02
added MatrixMedian.cpp
May 29, 2019
cdde068
better and smaller Solution for Trees/MinimumDepthOfABinaryTree.cpp
May 29, 2019
b013f0b
Update 3Sum.cpp
mayankm276 May 31, 2019
075ede3
Update PrettyJson.cpp
rushitjasani Jun 9, 2019
40cb733
Solution to kingdom War problem
Shivansh1805 Jun 14, 2019
718ca9c
Rename Kingdom War to KingdomWar
me-ydv-5 Jun 15, 2019
d61f675
Merge pull request #41 from Shivansh1805/patch-1
me-ydv-5 Jun 15, 2019
4774461
Update PrettyJson
me-ydv-5 Jun 15, 2019
cd922d4
Delete MatrixMedian.cpp
me-ydv-5 Jun 15, 2019
8a75518
Delete MinimumDepthOfABinaryTree.cpp
me-ydv-5 Jun 15, 2019
509b957
Rename SortedPermutatioRank.cpp to SortedPermutationRank.cpp
me-ydv-5 Jun 15, 2019
d848907
Merge pull request #37 from spiderOO7/master
me-ydv-5 Jun 15, 2019
5ad6cac
Min Depth of Binary Tree
me-ydv-5 Jun 15, 2019
2f28ef1
Merge pull request #38 from mayankm276/patch-1
me-ydv-5 Jun 15, 2019
f06e792
added find permutation
v1k22 Jun 28, 2019
bb51ac5
much simpler code than what was already here
v1k22 Jun 29, 2019
d444c8a
Rotated Sorted Array Search
hardikrana11 Jul 7, 2019
142ebaa
Palindrome String
hardikrana11 Jul 7, 2019
f0f3641
Merge pull request #42 from BhavikDhandhalya/patch-1
mailtokartik1 Jul 19, 2019
603f71b
Facebook Microsoft
sainimohit23 Jul 25, 2019
388cade
Merge pull request #43 from BhavikDhandhalya/patch-2
Jul 29, 2019
8e1bb67
Merge pull request #44 from pockemon/master
Jul 29, 2019
de16e21
Merge pull request #50 from sainimohit23/master
mailtokartik1 Jul 29, 2019
70439a8
Merge pull request #45 from pockemon/master
mailtokartik1 Jul 29, 2019
0657594
Added CombinationSum.cpp
aashish-ak Aug 3, 2019
8714d3d
Merge pull request #51 from aashish-ak/master
mailtokartik1 Aug 15, 2019
688df2f
Added CloneGraph.cpp (Google, Facebook, Amazon)
ssokjin Aug 16, 2019
cca485c
Merge pull request #54 from ssokjin/Added-Clone-Graph
mailtokartik1 Aug 23, 2019
f5feb5c
Numbers of Length N solution
mailtokartik1 Apr 30, 2020
7fade55
Merge pull request #74 from mailtokartik1/master
Apr 30, 2020
3eea0fc
add sorted permutation with repeats sultion
mailtokartik1 Apr 30, 2020
0d5ca11
Merge pull request #75 from mailtokartik1/master
Apr 30, 2020
f005d6c
add alternative solution using recursion for valid ip address
mailtokartik1 May 3, 2020
84166f6
Merge pull request #76 from mailtokartik1/master
May 3, 2020
8ec48df
add Stringoholics solution
mailtokartik1 May 5, 2020
88c0216
Merge pull request #77 from mailtokartik1/master
May 5, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Arrays/Find Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
Solution by : Bhavik Dhandhalya
Given a positive integer n and a string s consisting only of letters D or I, you have to find any permutation of first n positive integer that satisfy the given input string.

D means the next number is smaller, while I means the next number is greater.

Notes
Length of given string s will always equal to n - 1
Your solution should run in linear time and space.

Input 1:

n = 3
s = ID
Return: [1, 3, 2]

Solution Complexity : O(N) time and O(1) space
Refer this URL for lexicographically smaller permutation solution:
https://leetcode.com/articles/find-permutation/
*/

vector<int> Solution::findPerm(const string A, int B) {
vector < int > ans;
int n = A.length();
int maxi = n + 1, mini = 1;
for (int i = 0; i < n; i++) {
if (A[i] == 'I') ans.push_back(mini++);
if (A[i] == 'D') ans.push_back(maxi--);
}
ans.push_back(maxi);
return ans;
}
51 changes: 51 additions & 0 deletions Arrays/Flip simple code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
You are given a binary string(i.e. with characters 0 and 1) S consisting of characters S1, S2, …, SN. In a single operation, you can choose two indices L and R such that 1 ≤ L ≤ R ≤ N and flip the characters SL, SL+1, …, SR. By flipping, we mean change character 0 to 1 and vice-versa.

Your aim is to perform ATMOST one operation such that in final string number of 1s is maximised. If you don’t want to perform the operation, return an empty array. Else, return an array consisting of two elements denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.

Notes:

Pair (a, b) is lexicographically smaller than pair (c, d) if a < c or, if a == c and b < d.
For example,
S = 010
Output = [1 1]
*/

vector<int> Solution::flip(string A) {
int n = A.length();
int ones = 0;
int zeros = 0;
int L = INT_MAX, R = 0;
int ansL = 0, ansR = 0;
int prev = 0;
bool found = false;

for (int i = 0; i < n; i++) {
ones += A[i] == '1';
zeros += A[i] == '0';
if (zeros) found = true;

if (ones > zeros) {
ones = 0;
zeros = 0;
L = INT_MAX;
R = 0;
} else if (A[i] == '0') {
L = min(L, i);
R = i;

if (zeros - ones > prev) {
ansR = R;
ansL = L;
prev = zeros - ones;
}
}
}

vector < int > ans;
if (found) {
ans.push_back(ansL + 1);
ans.push_back(ansR + 1);
}
return ans;
}
107 changes: 73 additions & 34 deletions Arrays/MaxDistance.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,80 @@
// https://www.interviewbit.com/problems/max-distance/

int Solution::maximumGap(const vector<int> &A) {
// Base case
if(A.size() <= 1)return 0;

// Make two arrays such that one of them holds the minimum from the left
// and the other holds maximum from the right
vector<int> v1 (A.size(), 0), v2(A.size(), 0);

// Fill the first array with minimum from the left
v1[0] = A[0];
for(int i = 1 ; i < A.size(); i++)v1[i] = min(v1[i-1], A[i]);

// Fill the second array with maximum from the right
v2[A.size()-1] = A[A.size()-1];
for(int i = A.size()-2 ; i >= 0 ; i--)v2[i] = max(v2[i+1], A[i]);


int i = 0, j = 0;
int ans = -1;
// While we don't traverse the complete array, check if the minimum element is indeed
// less than the maximum element in the other array, if yes, update the answer.
// Move the pointer as required. If the element in second array is greater than
// that in first array, keep moving the second pointer and update the answer
// else move the first pointer.
while(j < A.size() && i < A.size()){
if(v2[j] >= v1[i]){
if(j-i > ans){
ans = j-i;
}
j = j + 1;
}else i = i + 1;
}
return ans;
}




// Here we are making an array to check whether all the elements on the left of an index are smaller than it or not
// If true then we are checking for greater j such that A[j] is greater than A[i]. If so then that j is greater than all i on the left
// also. On finding such a j or a false in the check array, we decrement i.

int Solution::maximumGap(const vector<int> &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
vector<bool> tempArr(A.size());
int min = A[0];
int max_distance = 0;
int i = A.size()-1;
int j = A.size()-1;
// int Solution::maximumGap(const vector<int> &A) {
// // Do not write main() function.
// // Do not read input, instead use the arguments to the function.
// // Do not print the output, instead return values as specified
// // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
// vector<bool> tempArr(A.size());
// int min = A[0];
// int max_distance = 0;
// int i = A.size()-1;
// int j = A.size()-1;

for(int i = 0; i < A.size(); i++){
if(A[i] > min){
tempArr[i] = false;
}
else{
min = A[i];
tempArr[i] = true;
}
}
// for(int i = 0; i < A.size(); i++){
// if(A[i] > min){
// tempArr[i] = false;
// }
// else{
// min = A[i];
// tempArr[i] = true;
// }
// }

LOOP:while(i >= 0){
if(tempArr[i] == false){
i--;
goto LOOP;
}
while((A[i] > A[j]) && (j > i)){
j--;
}
if((j-i) > max_distance){
max_distance = j-i;
}
i--;
}
return max_distance;
}
// LOOP:while(i >= 0){
// if(tempArr[i] == false){
// i--;
// goto LOOP;
// }
// while((A[i] > A[j]) && (j > i)){
// j--;
// }
// if((j-i) > max_distance){
// max_distance = j-i;
// }
// i--;
// }
// return max_distance;
// }
19 changes: 19 additions & 0 deletions Arrays/MinStepsInInfiniteGrid.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// https://www.interviewbit.com/problems/min-steps-in-infinite-grid/
// Input : X and Y co-ordinates of the points in order.
// Each point is represented by (X[i], Y[i])

// Explanatory code
/*
int Solution::coverPoints(vector<int> &X, vector<int> &Y) {

int steps = 0, dx, dy, i = 0;
Expand Down Expand Up @@ -38,3 +41,19 @@ int Solution::coverPoints(vector<int> &X, vector<int> &Y) {

return steps;
}
*/

// Concise code
int coverPoints(vector<int> &X, vector<int> &Y) {

int size1=X.size(),size2=Y.size(),ans=0;

for(int i=1;i<size1;i++)

{
ans = ans + (abs(X[i]-X[i-1])<abs(Y[i]-Y[i-1])?abs(Y[i]-Y[i-1]):abs(X[i]-X[i-1]));
}

return ans;
}

27 changes: 27 additions & 0 deletions Backtracking/CombinationSum.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
void aux(int i, int sum, int k, vector<int> &A, vector<int> &temp, set<vector<int>> &ans) {
if(i == A.size() || sum >= k){
if(sum == k){
ans.insert(temp);
}
return;
}

temp.push_back(A[i]);
aux(i, sum+A[i], k, A, temp, ans);
aux(i+1, sum+A[i], k, A, temp, ans);
temp.pop_back();
aux(i+1, sum, k, A, temp, ans);
}

vector<vector<int> > Solution::combinationSum(vector<int> &A, int B) {
set<vector<int>> ans;
sort(A.begin(), A.end());
vector<int> temp;
aux(0, 0, B, A, temp, ans);
vector<vector<int>> res;

for(auto i = ans.begin(); i != ans.end(); i++){
res.push_back(*i);
}
return res;
}
50 changes: 50 additions & 0 deletions Backtracking/NQueens.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
vector<vector<string> >ans;
int n;

void recur(int ind, vector<int> pos)
{
int i,j;
if(ind == n)
{
vector<string> valid;
for(i=0;i<n;i++)
{
string row = "";
for(j=0;j<n;j++)
{
if(j == pos[i])
row += 'Q';
else
row += '.';
}
valid.push_back(row);
}
ans.push_back(valid);

return;
}
for(i = 0;i<n;i++)
{
bool flag = true;
for(j=0;j<pos.size();j++)
{
if(abs(ind-j) == abs(pos[j]-i) || i == pos[j])
flag = false;
}
if(!flag)
continue;
pos.push_back(i);
recur(ind+1,pos);
pos.pop_back();
//pos[ind] = -1;
}
}
vector<vector<string> > Solution::solveNQueens(int A)
{
ans.clear();
vector<int> pos;
int i;
n = A;
recur(0,pos);
return ans;
}
39 changes: 39 additions & 0 deletions Binary-Search/RotatedSortedArraySearch.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
// https://www.interviewbit.com/problems/rotated-sorted-array-search/

int search1(const vector<int> &arr, int low, int high, int B)
{
if (low > high) return -1;

int mid = (low) + (high-low)/2;
if (arr[mid] == B) return mid;

if (arr[low] <= arr[mid])
{
if (B >= arr[low] && B <= arr[mid])
return search1(arr, low, mid-1, B);

return search1(arr, mid+1, high, B);
}

else if (arr[mid] <= arr[high])
{
if (B >= arr[mid] && B <= arr[high])
return search1(arr, mid+1, high, B);

return search1(arr, low, mid-1, B);
}
}


int Solution::search(const vector<int> &A, int B) {

int n = A.size();
int i = search1(A, 0, n-1, B);

if (i != -1)
return i;
else
return -1;

}

/*
int findPivot(const vector<int> &A){
int start = 0;
int end = A.size()-1;
Expand Down Expand Up @@ -64,3 +102,4 @@ int Solution::search(const vector<int> &A, int B) {
// B < A[pivot]

}
*/
18 changes: 18 additions & 0 deletions Bit-Manipulation/DivideInteger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
int Solution::divide(int a, int b) {
if(a==0)
return 0;
if(b==0)
return (INT_MAX);
int sign=1;
//remove sign of operands
int t=0,q=0;
a=abs(a);
b=abs(b);
for(int i=31;i>=0;--i){
if(t+(b<<i)<=a){
t+=b<<i;
q+=1LL<<i;
}
}
return (sign*q>=INT_MAX||sign*q<INT_MIN)?INT_MAX:sign*q;
}
Loading