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
int findKthLargest(vector<int>& nums, int k) { | |
priority_queue<int, vector<int>, greater<int>> pq; | |
for (int num : nums) { | |
pq.push(num); | |
if (pq.size() > k) { | |
pq.pop(); | |
} | |
} | |
return pq.top(); | |
} |
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
int ceilSearch(int arr[], int low, int high, int x) | |
{ | |
if(x <= arr[low]) return low; | |
if(x > arr[high]) return -1; | |
int mid = low + (high - low)/2; | |
if(arr[mid] == x) | |
return mid; | |
/* If x is greater than arr[mid], then either arr[mid + 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
int findRight(const ArrayReader& reader, int target){ | |
int right = 1; | |
while (reader.get(right) < target) | |
right = right * 2; | |
return right; | |
} | |
int searchInfiniteArray(const ArrayReader& reader, int target) { | |
int left = 0, right = findRight(reader, target); | |
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
//Iteration | |
vector<vector<int>> permute(vector<int>& nums) { | |
vector<vector<int>> res; | |
queue<vector<int>> q; | |
q.push(vector<int>()); | |
for(auto num : nums){ | |
// we will take all existing permutations and add the current number to create new | |
// permutations | |
int n = q.size(); | |
for(int i = 0; i < n; 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
void helper(string &s, int l, vector<string> &v){ | |
if(l == s.size()){ | |
v.push_back(s); | |
return; | |
} | |
helper(s, l+1, v); | |
if(isalpha(s[l])){ | |
s[l] ^= (1<<5); //toggle upper to lower, lower to upper | |
helper(s, l+1, v); | |
s[l] ^= (1<<5); //restore, it works without this line, but to leave the origin string unchanged, better to have it. |
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
vector<vector<int>> subsetsWithDup(vector<int>& nums) { | |
vector<vector<int>> res{{}}; // same as: vector<vector<int>> res; res.push_back(vector<int>()); | |
int start = 0, end = 0; | |
sort(nums.begin(), nums.end()); | |
for(int i = 0; i < nums.size(); i++){ | |
start = 0; | |
if(i > 0 && nums[i] == nums[i-1]) | |
start = end + 1; | |
end = res.size() - 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
// Iteration | |
vector<vector<int>> subsets(vector<int>& nums) { | |
vector<vector<int>> vv; | |
vv.push_back(vector<int>()); | |
for(int i = 0; i < nums.size(); i++){ | |
int n = vv.size(); | |
for(int j = 0; j < n; j++){ | |
vector<int> v(vv[j]); | |
v.push_back(nums[i]); | |
vv.push_back(v); |
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
void helper(TreeNode* root, vector<vector<int>> &res, vector<int>&vec, int sum){ | |
if(!root)return; | |
vec.push_back(root->val); | |
if(!root->left && !root->right && root->val==sum) | |
res.push_back(vec); | |
helper(root->left, res, vec, sum-root->val); | |
helper(root->right, res, vec, sum-root->val); | |
vec.pop_back(); | |
} | |
vector<vector<int>> pathSum(TreeNode* root, int sum) { |
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
// Iteration | |
bool hasPathSum(TreeNode* root, int sum) { | |
if(!root) return false; | |
stack<TreeNode*>s{{root}}; | |
while(!s.empty()){ | |
TreeNode* tmp = s.top(); | |
s.pop(); | |
if(!tmp->left && !tmp->right && tmp->val == sum) | |
return true; | |
if(tmp->left){ |
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
// Iteration | |
vector<vector<int> > levelOrderBottom(TreeNode* root) { | |
if (!root) return {}; | |
vector<vector<int>> res; | |
queue<TreeNode*> q{{root}}; | |
while (!q.empty()) { | |
vector<int> oneLevel; | |
for (int i = q.size(); i > 0; --i) { //init i with q.size(), it runs well even if we change the size of q in loop | |
TreeNode *t = q.front(); | |
q.pop(); |