My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
delete-node-in-a-bst(medium)
First, find the node which should be deleted.
(1) If the node is a leaf node, delete it directly.
(2) If the node has exactly one child, replace it with its child.
(3) If the node has two child, we will find the largest element in its left subtree, and replace it with that element. This can be implemented using recursion of deleting an element in its left subtree.
- Time complexity: O(height).
- Space complexity: O(1).
flatten-a-multilevel-doubly-linked-list(easy)
There are some pitfalls: (1) make nodes’ child pointers to NULL; (2) the same instance can be used for many times.
- Time complexity: O(n).
- Space complexity: O(1).
knight-probability-in-chessboard(easy)
Like DP, cs[i + 1][nx][ny] += 1.0 * cs[i][x][y] / 8, where cs[i][x][y] denotes the probability of the knight staying at (x,y) after i moves
.
- Time complexity: O(KN^2).
- Space complexity: O(KN^2) without compressing state space.
kth-largest-element-in-a-stream(medium)
k never changes, so we can use min-heap to maintain the k largest numbers. Or, we should use BST.
- Time complexity: O(logn) for add.
- Space complexity: O(n).
largest-palindrome-product(medium)
I do not have any good idea for this problem, although I passed all test cases using brute force whose time complexity can be very large theoretically.
- Time complexity: hard to tell.
- Space complexity: O(n).
lowest-common-ancestor-of-a-binary-search-tree(easy)
DFS.
- Time complexity: O(n).
- Space complexity: O(n).
maximum-size-subarray-sum-equals-k(medium)
Same as 2sum by converting sum of a subarray to the difference of two prefix sums.
- Time complexity: O(n).
- Space complexity: O(n).
range-sum-query-2d-mutable(hard)
2D-bits.
- Time complexity: O(logn*logn) for each add and sumRegion functions, O((nlogn)^2) for initialization.
- Space complexity: O(n^2).
reverse-bits(easy)
Bits operations.
- Time complexity: O(32).
- Space complexity: O(1).
reverse-words-in-a-string-iii(easy)
Find each word and reverse it.
- Time complexity: O(n).
- Space complexity: O(1).
rotate-array(medium)
(1) push_back first (n-k) numbers, then move forwards elements by (n-k) positions.
(2) Cyclic Replacements without using extra space. The trick is about how do we know whether a number has been swapped.
(3) Reverse three times.
- Time complexity: O(n).
- Space complexity: O(n) or O(1).
sort-array-by-parity(easy)
Like partition in quick-sort.
- Time complexity: O(n).
- Space complexity: O(1).
strobogrammatic-number-ii(easy)
DFS and use an argument to denote whether we can use 0
.
- Time complexity: O(5^(n/2)*n).
- Space complexity: O(^(n/2)*n).
strobogrammatic-number-iii(hard)
DFS to calculate how many numbers whose value are less than s
and whose length is less than len
. Also we use an argument to denote whether we can use 0
.
And there are many important details in the implementation.
- Time complexity: O(length).
- Space complexity: O(length).
unique-binary-search-trees-ii(medium)
DFS to build all trees of the range of [min_number, max_number].
- Time complexity: hard to tell, but must be exponential complexity.
- Space complexity: same as Time complexity.
unique-paths-ii(easy)
Simplest DP.
- Time complexity: O(row*col).
- Space complexity: O(row*col).
valid-palindrome-ii(easy)
There will be only one decision we should make.
- Time complexity: O(n).
- Space complexity: O(1).