forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
algorithms/kthSmallestElementInaBST/KthSmallestElementInABst.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Source : https://leetcode.com/problems/kth-smallest-element-in-a-bst/ | ||
// Author : Hao Chen | ||
// Date : 2015-07-03 | ||
|
||
/********************************************************************************** | ||
* | ||
* Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. | ||
* | ||
* Note: | ||
* You may assume k is always valid, 1 ≤ k ≤ BST's total elements. | ||
* | ||
* Follow up: | ||
* What if the BST is modified (insert/delete operations) often and you need to find the kth smallest frequently? | ||
* How would you optimize the kthSmallest routine? | ||
* | ||
* Try to utilize the property of a BST. | ||
* What if you could modify the BST node's structure? | ||
* The optimal runtime complexity is O(height of BST). | ||
* | ||
* Credits:Special thanks to @ts for adding this problem and creating all test cases. | ||
**********************************************************************************/ | ||
|
||
|
||
/** | ||
* Definition for a binary tree node. | ||
* struct TreeNode { | ||
* int val; | ||
* TreeNode *left; | ||
* TreeNode *right; | ||
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | ||
* }; | ||
*/ | ||
class Solution { | ||
public: | ||
// in-order travel - recursive way | ||
int kthSmallestHelper_recursive(TreeNode* root, int& k) { | ||
if (root==NULL) return 0; //this behavior is undefined! | ||
|
||
//in-order travel | ||
int result = kthSmallestHelper_recursive(root->left, k); | ||
if (k==0) return result; | ||
|
||
k--; | ||
if (k==0) return root->val; | ||
|
||
|
||
return kthSmallestHelper_recursive(root->right, k); | ||
} | ||
// in-order travel - non-recursive way | ||
int kthSmallestHelper_nonRecursive(TreeNode* root, int k){ | ||
stack<TreeNode*> s; | ||
|
||
while(!s.empty() || root){ | ||
|
||
while (root) { | ||
s.push(root); | ||
root = root->left; | ||
} | ||
|
||
k--; | ||
root = s.top()->right; | ||
|
||
if (k==0) return s.top()->val; | ||
|
||
s.pop(); | ||
} | ||
return -1; | ||
} | ||
|
||
int kthSmallest(TreeNode* root, int k) { | ||
//return kthSmallestHelper_nonRecursive(root, k); | ||
return kthSmallestHelper_recursive(root, k); | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Source : https://leetcode.com/problems/majority-element-ii/ | ||
// Author : Hao Chen | ||
// Date : 2015-07-03 | ||
|
||
/********************************************************************************** | ||
* | ||
* Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. | ||
* The algorithm should run in linear time and in O(1) space. | ||
* | ||
* How many majority elements could it possibly have? | ||
* Do you have a better hint? Suggest it! | ||
**********************************************************************************/ | ||
|
||
class Solution { | ||
public: | ||
|
||
//O(n) Space compexity | ||
vector<int> majorityElement01(vector<int>& nums) { | ||
vector<int> result; | ||
unordered_map<int, int> counts; | ||
int n = nums.size(); | ||
for(auto item : nums){ | ||
counts[item]++; | ||
if (counts[item] > n/3 ){ | ||
result.push_back(item); | ||
counts[item] = -n; // Tricky: make sure the item only can be put into result once. | ||
} | ||
} | ||
return result; | ||
} | ||
//We know, there could be at most two numbers can be more than 1/3 | ||
//so, same as Majority Element I problem, we can have two counters. | ||
vector<int> majorityElement02(vector<int>& nums) { | ||
if(nums.size()<=1) return nums; | ||
|
||
//the same algorithm as Majority Element I problem | ||
int majority1=0, majority2=0, cnt1=0, cnt2=0; | ||
for(auto item: nums) { | ||
if (cnt1 == 0) { | ||
majority1 = item; | ||
cnt1 = 1; | ||
} else if (majority1 == item) { | ||
cnt1++; | ||
} else if (cnt2 == 0) { | ||
majority2 = item; | ||
cnt2 = 1; | ||
} else if (majority2 == item) { | ||
cnt2++; | ||
} else { | ||
cnt1--; | ||
cnt2--; | ||
} | ||
} | ||
//re-check it again, in case there has less than two numbers of majority | ||
cnt1 = cnt2 = 0; | ||
for (auto item : nums) { | ||
if (majority1 == item) cnt1++; | ||
else if (majority2 == item) cnt2++; | ||
} | ||
vector<int> result; | ||
if (cnt1 > nums.size()/3) result.push_back(majority1); | ||
if (cnt2 > nums.size()/3) result.push_back(majority2); | ||
return result; | ||
|
||
} | ||
|
||
vector<int> majorityElement(vector<int>& nums) { | ||
return majorityElement02(nums); | ||
return majorityElement01(nums); | ||
} | ||
}; | ||
|
||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// Source : https://leetcode.com/problems/summary-ranges/ | ||
// Author : Hao Chen | ||
// Date : 2015-07-03 | ||
|
||
/********************************************************************************** | ||
* | ||
* Given a sorted integer array without duplicates, return the summary of its ranges. | ||
* | ||
* For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"]. | ||
* | ||
* Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases. | ||
* | ||
**********************************************************************************/ | ||
|
||
|
||
class Solution { | ||
public: | ||
string makeRange(int start, int end) { | ||
ostringstream oss; | ||
if (start != end) { | ||
oss << start << "->" << end; | ||
} else { | ||
oss << start; | ||
} | ||
return oss.str(); | ||
} | ||
|
||
vector<string> summaryRanges(vector<int>& nums) { | ||
vector<string> result; | ||
int len = nums.size(); | ||
if (len == 0) return result; | ||
|
||
// we have two pointer for range-starter and range-ender | ||
int start=nums[0], end=nums[0]; | ||
|
||
for (int i=1; i<len; i++) { | ||
// if it is continous number, move the end pointer; | ||
if (nums[i] == end + 1) { | ||
end = nums[i]; | ||
continue; | ||
} | ||
|
||
//if the number is not continous, push the range into result | ||
//and reset the start and end pointer | ||
result.push_back(makeRange(start, end)); | ||
start = end = nums[i]; | ||
|
||
} | ||
|
||
//for the last range | ||
result.push_back(makeRange(start, end)); | ||
|
||
return result; | ||
} | ||
}; |