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 <vector> | |
using namespace std; | |
// O(n * 2^n) time | O(n * 2^n) space | |
vector<vector<int>> helper(vector<int> array, int idx) { | |
if (idx < 0) return {{}}; // Return empty array | |
vector<vector<int>> subsets = helper(array, idx - 1); | |
int length = subsets.size(); | |
int word = array[idx]; // starts from last element | |
for (int j = 0; j < length; 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
#include <vector> | |
using namespace std; | |
vector<int> getBiggerOrEqual(vector<int> array) { | |
vector<int> biggerOrEqual = {}; | |
for (int i = 1; i < array.size(); i++) { | |
if (array[i] >= array[0]) { | |
biggerOrEqual.push_back(array[i]); | |
} |
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 <vector> | |
#include <algorithm> | |
#include <unordered_map> | |
using namespace std; | |
vector<vector<string>> groupAnagrams(vector<string> words) { | |
vector<vector<string>> response; | |
unordered_map<string, vector<string>> tables; | |
for (string word : words) { |
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
using namespace std; | |
vector<int> compareHelper(string str, int startIndex, int endIndex) { | |
while (startIndex >= 0 && endIndex < str.size()) { | |
if (str[startIndex] != str[endIndex]) { | |
break; | |
} | |
startIndex--; | |
endIndex++; | |
} |
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 getBinaryDigit = (target, bytes = 31) => { | |
let result = ""; | |
for(let i = 0; i < bytes; i++) { | |
const condition = target & (1 << i); | |
if (condition != 0) { | |
result = 1 + result; | |
} else { | |
result = 0 + result; | |
} | |
} |
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 <vector> | |
#include <unordered_map> | |
using namespace std; | |
int distanceBetwen(int a, int b) { | |
return abs(a - b); | |
} | |
int getIndexOfMinValue(vector<int>& arr) { |
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 <vector> | |
#include <unordered_map> | |
#include <climits> | |
using namespace std; | |
vector<int> largestRange(vector<int> array) { | |
vector<int> longestRange; | |
unordered_map<int, bool> arrayMap; | |
int longestLength = INT_MIN; |
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 <vector> | |
#include <climits> | |
using namespace std; | |
bool isInOrder(int i, vector<int>& array) { | |
// Left most | |
if (i == 0) return array[i] < array[i + 1]; | |
// Right most | |
if (i == array.size() - 1) return array[i] > array[i - 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
#include <vector> | |
#include <unordered_map> | |
using namespace std; | |
vector<vector<int>> fourNumberSum(vector<int> array, int targetSum) { | |
// Write your code here | |
unordered_map<int, vector<vector<int>>> allPairSums; | |
vector<vector<int>> quadruplets{}; | |
// For every element apply the next algorithm |
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 <vector> | |
using namespace std; | |
// METHOD 1 | |
// Ypper Bound: O(n! * n * n) time | O(n*n!) space | |
void getPermutations(vector<int> array, vector<int> currentPerm, vector<vector<int>>& permutations) { | |
if (array.empty() && currentPerm.size() > 0) { | |
permutations.push_back(currentPerm); // This get fired n! times | |
} |