-
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
16 changed files
with
1,028 additions
and
3 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#+title: Morris Traversal | ||
|
||
A O(1) space, in-order, binary tree traversal algorithm. | ||
|
||
* Tree node definition | ||
|
||
#+begin_src C++ | ||
struct TreeNode { | ||
int val; | ||
TreeNode *left; | ||
TreeNode *right; | ||
}; | ||
#+end_src | ||
|
||
Given the root of the tree =root=, traverse the tree using constant space. | ||
|
||
* Basic Idea | ||
|
||
Modify a leaf node's right child to point to it's in-order successor. | ||
|
||
#+begin_src C++ | ||
void morris(TreeNode* root) { | ||
while(root) { | ||
if (!root->left) { | ||
visit(root); | ||
root = root->right; | ||
} else { | ||
TreeNode* t = root->left; | ||
while(t->right && t->right != root) t = t->right; | ||
if (!t->right) { | ||
// update t->right to point to root | ||
t->right = root; | ||
root = root->left; | ||
} else { | ||
// visited all root->left tree | ||
visit(root); | ||
t->right = nullptr; | ||
root = root->right; | ||
} | ||
} | ||
} | ||
} | ||
#+end_src | ||
|
||
* Application | ||
C++ destructor of a Tree may cause stackoverflow if the tree is not well balanced if we recursively call the destructor of each children. | ||
Using Morris Traversal can eliminate this problem | ||
|
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,18 @@ | ||
#+title: Distinct Subsequences | ||
|
||
Given two strings s and t, return the number of distinct subsequences of s which equals t. | ||
|
||
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 | ||
characters' relative positions. (i.e., "ACE" is a subsequence of "ABCDE" while "AEC" is not). | ||
|
||
The test cases are generated so that the answer fits on a 32-bit signed integer. | ||
|
||
* Recursive call + memoization | ||
|
||
Let \(f(i, j)\) be the number of distinct subsequences of s[i:] that equal t[j:], now what does \(f\) look like? | ||
|
||
1. if \(j = length(t)\), the value is 1, as we have reached the end of string t. | ||
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. | ||
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 | ||
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. | ||
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. |
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,9 @@ | ||
#+title: Longest Consecutive Sequence | ||
|
||
Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. | ||
|
||
You must write an algorithm that runs in O(n) time. | ||
|
||
* Solution: hash set | ||
|
||
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. |
19 changes: 19 additions & 0 deletions
19
Algorithms/Leetcode/0302.smallest.rectangle.encloing.black.pixels.org
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,19 @@ | ||
#+title: Smallest Rectangle Enclosing Black Pixels | ||
|
||
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= | ||
find the area of the smallest rectangle that encloses all the black pxiels. | ||
|
||
Time complexity must be less than =O(mn)=. | ||
|
||
* Analysis | ||
|
||
To Calculate the area, we need four numbers: (left, top) - (right, bottom) | ||
|
||
- top: minimum of y | ||
- bottom: maximum of y | ||
- left: minimum of x | ||
- right: maximum of x | ||
|
||
- =O(mn)= is easy: DFS/BFS, and update the four numbers along the way. | ||
|
||
- Is there a way to not go through all the nodes? |
Oops, something went wrong.