Skip to content

Commit

Permalink
Three New Problems
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Jul 3, 2015
1 parent 5f8266d commit 8edd75c
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ LeetCode

| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|214|[Kth Smallest Element in a BST](https://leetcode.com/problems/kth-smallest-element-in-a-bst/)| [C++](./algorithms/kthSmallestElementInaBST/KthSmallestElementInABst.cpp)|Medium|
|213|[Majority Element II](https://oj.leetcode.com/problems/majority-element-ii/) | [C++](./algorithms/majorityElement/majorityElement.II.cpp)|Medium|
|212|[Summary Ranges](https://leetcode.com/problems/summary-ranges/)| [C++](./algorithms/summaryRanges/SummaryRanges.cpp)|Easy|
|211|[Basic Calculator II](https://leetcode.com/problems/basic-calculator-ii/)| [C++](./algorithms/basicCalculator/BasicCalculator.II.cpp)|Medium|
|210|[Invert Binary Tree](https://leetcode.com/problems/invert-binary-tree/)| [C++](./algorithms/invertBinaryTree/InvertBinaryTree.cpp)|Easy|
|209|[Implement Stack using Queues](https://leetcode.com/problems/implement-stack-using-queues/)| [C++](./algorithms/implementStackUsingQueues/ImplementStackUsingQueues.cpp)|Medium|
Expand Down
74 changes: 74 additions & 0 deletions algorithms/kthSmallestElementInaBST/KthSmallestElementInABst.cpp
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);
}
};
73 changes: 73 additions & 0 deletions algorithms/majorityElement/majorityElement.II.cpp
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);
}
};


2 changes: 1 addition & 1 deletion algorithms/majorityElement/majorityElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ int majorityElement(vector<int> &num) {
cnt++;
}else{
majority == num[i] ? cnt++ : cnt --;
if (cnt >= num.size()/2+1) return majority;
if (cnt >= num.size()/2) return majority;
}
}
return majority;
Expand Down
55 changes: 55 additions & 0 deletions algorithms/summaryRanges/SummaryRanges.cpp
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;
}
};

0 comments on commit 8edd75c

Please sign in to comment.