Skip to content

Commit da58092

Browse files
committed
8 Mar 2022
1 parent 492ea22 commit da58092

16 files changed

+1028
-3
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#+title: Morris Traversal
2+
3+
A O(1) space, in-order, binary tree traversal algorithm.
4+
5+
* Tree node definition
6+
7+
#+begin_src C++
8+
struct TreeNode {
9+
int val;
10+
TreeNode *left;
11+
TreeNode *right;
12+
};
13+
#+end_src
14+
15+
Given the root of the tree =root=, traverse the tree using constant space.
16+
17+
* Basic Idea
18+
19+
Modify a leaf node's right child to point to it's in-order successor.
20+
21+
#+begin_src C++
22+
void morris(TreeNode* root) {
23+
while(root) {
24+
if (!root->left) {
25+
visit(root);
26+
root = root->right;
27+
} else {
28+
TreeNode* t = root->left;
29+
while(t->right && t->right != root) t = t->right;
30+
if (!t->right) {
31+
// update t->right to point to root
32+
t->right = root;
33+
root = root->left;
34+
} else {
35+
// visited all root->left tree
36+
visit(root);
37+
t->right = nullptr;
38+
root = root->right;
39+
}
40+
}
41+
}
42+
}
43+
#+end_src
44+
45+
* Application
46+
C++ destructor of a Tree may cause stackoverflow if the tree is not well balanced if we recursively call the destructor of each children.
47+
Using Morris Traversal can eliminate this problem
48+

Algorithms/Leetcode/0042.trapping.rain.water.org

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,54 @@
66
right that's higher than that at position i. Now if we maintain a max height from both sides, and if they are both higher than position i, we know some water
77
can be trapped at position i, the amount is the minimum of the max heights minus the height at i.
88

9-
One pointer move from left to right starting at 0, while the other from right to left starting at n - 1.
10-
We also keep track of max heights the two pointers have seen.
9+
One pointer move from left to right starting at 0, while the other from right to left starting at n - 1. We also keep track of max heights the two pointers
10+
have seen.
1111

12-
12+
Time Complexity: O(n), every height is accessed at most twice.
13+
14+
#+begin_src C++
15+
int waterTrapped(vector<int>& heights) {
16+
int n = heights.size();
17+
int l = 0, r = n - 1;
18+
int lmax = -1, rmax = -1;
19+
int water = 0;
20+
while (l < r) {
21+
lmax = max(lmax, heights[l]);
22+
rmax = max(rmax, heights[r]);
23+
if (lmax < rmax) {
24+
water += lmax - heights[l++];
25+
} else {
26+
water += rmax - heights[r--];
27+
}
28+
}
29+
return water;
30+
}
31+
#+end_src
1332

1433

34+
* Solution 2: min-stack
35+
Using a stack to keep track of a decreasing sequence of heights, if the next height is higher than the top of the stack and the stack has more than one
36+
element, then some water can be trapped in between current height and the one next to the top. How much? It's the number of heights between current and the
37+
height next to the current.
38+
39+
Time complexity: O(n)
40+
Space complexity: O(n), there could be at most n elements in the stack.
41+
42+
#+begin_src C++
43+
int trap(vector<int>& heights) {
44+
int n = heights.size();
45+
int water = 0;
46+
stack<int> stk;
47+
for(int i = 0; i < n; ++i) {
48+
int h = heights[i];
49+
while(!stk.empty() && heights[stk.top()] < h) {
50+
int top = stk.top(); stk.pop();
51+
if (stk.empty()) break;
52+
int blockHeight = min(heights[stk.top()], h) - heights[top];
53+
water += blockHeight * (i - stk.top() - 1);
54+
}
55+
stk.push(i);
56+
}
57+
return water;
58+
}
59+
#+end_src
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#+title: Distinct Subsequences
2+
3+
Given two strings s and t, return the number of distinct subsequences of s which equals t.
4+
5+
A string's subsequence is a new string formed from the original string by deleting some (can be none) of the characters without disturbing the remaining
6+
characters' relative positions. (i.e., "ACE" is a subsequence of "ABCDE" while "AEC" is not).
7+
8+
The test cases are generated so that the answer fits on a 32-bit signed integer.
9+
10+
* Recursive call + memoization
11+
12+
Let \(f(i, j)\) be the number of distinct subsequences of s[i:] that equal t[j:], now what does \(f\) look like?
13+
14+
1. if \(j = length(t)\), the value is 1, as we have reached the end of string t.
15+
2. if \(i = length(s)\), the value is 0, i.e. there's no more characters in s, but still some "unmatched" characters in t.
16+
3. if \(s[i] = t[j]\), the value is \(f(i + 1, j + 1) + f(i + 1, j)\), that is the number of distinct subsequences of s[i+1:] that equal t[j+1:], plus the
17+
number of distinct subsequences of s[i+1:] that equal t[j:]. That is, we can skip both, or we can only skip one character in s.
18+
4. if \(s[i] != t[j]\), the value is \(f(i + 1, j)\), we cannot skip character in t, as we did not see a match.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#+title: Longest Consecutive Sequence
2+
3+
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.
4+
5+
You must write an algorithm that runs in O(n) time.
6+
7+
* Solution: hash set
8+
9+
If we keep a hashset that contains all elements from the input array, and we can test whether an element is in the set or not in roughly O(1) time.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#+title: Smallest Rectangle Enclosing Black Pixels
2+
3+
Given =mxn= binary matrix where =0= represents a white pixel and =1= a black pixel. Also given the location of one of the black pixels, =x= and =y=
4+
find the area of the smallest rectangle that encloses all the black pxiels.
5+
6+
Time complexity must be less than =O(mn)=.
7+
8+
* Analysis
9+
10+
To Calculate the area, we need four numbers: (left, top) - (right, bottom)
11+
12+
- top: minimum of y
13+
- bottom: maximum of y
14+
- left: minimum of x
15+
- right: maximum of x
16+
17+
- =O(mn)= is easy: DFS/BFS, and update the four numbers along the way.
18+
19+
- Is there a way to not go through all the nodes?

0 commit comments

Comments
 (0)