This file contains 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
/// https://www.geeksforgeeks.org/problems/tower-of-hanoi-1587115621/1 | |
class Solution{ | |
public: | |
// You need to complete this function | |
// avoid space at the starting of the string in "move disk....." | |
long long toh(int n, int from, int to, int aux) { | |
if(n==1){ | |
cout<<"move disk " << n << " from rod " <<from <<" to rod "<<to<<endl; |
This file contains 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
/////// Approach 1: Space Complexity: O(n) | |
class Solution{ | |
public: | |
void Reverse(stack<int> &St){ | |
if(St.empty()){ | |
return; | |
} | |
int top = St.top(); |
This file contains 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: | |
void merge(int arr[], int l, int m, int r) | |
{ | |
int n1 = m-l+1; | |
int n2 = r-m; | |
int L[n1], R[n2]; | |
int k =l; |
This file contains 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: | |
//Function to sort an array using quick sort algorithm. | |
void quickSort(int arr[], int low, int high) | |
{ | |
if(low>=high){ | |
return; | |
} | |
This file contains 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
map<int,int> mapInsert(int arr[],int n){ | |
map<int,int>mp; | |
//Insert arr[i] as key and i as value | |
for(int i=0; i<n; i++){ | |
mp[arr[i]] = i; | |
} | |
return mp; | |
} |
This file contains 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: | |
vector<vector<int>> generate(int numRows) { | |
vector<vector<int>> res(numRows); | |
for (int i = 0; i < numRows; i++) { | |
res[i] = vector<int>(i + 1, 1); | |
for (int j = 1; j < i; j++) { | |
res[i][j] = res[i-1][j]+ res[i-1][j-1]; |
This file contains 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: | |
void rotate(vector<vector<int>>& matrix) { | |
int row = matrix.size(); | |
int col = row; | |
for (int i = 0; i < row; i++) { | |
for (int j = i; j < col; j++) { | |
swap(matrix[i][j], matrix[j][i]); | |
} |
This file contains 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: | |
vector<int> spiralOrder(vector<vector<int>>& matrix) { | |
int m = matrix.size(); | |
int n = matrix[0].size(); | |
int top = 0; | |
int down = m - 1; | |
int left = 0; | |
int right = n - 1; |
This file contains 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: | |
vector<vector<string>> groupAnagrams(vector<string>& strs) { | |
int n = strs.size(); | |
unordered_map<string, vector<string>> mp; | |
for (int i = 0; i < n; i++) { | |
string temp = strs[i]; |
This file contains 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: | |
TreeNode* removeLeafNodes(TreeNode* root, int target) { | |
if (root == NULL) | |
return NULL; | |
root->left = removeLeafNodes(root->left, target); | |
root->right = removeLeafNodes(root->right, target); | |
if (root->left == NULL && root->right == NULL && root->val == target) |
OlderNewer