1 | /** |
Topological Sorting
DFS: O(n)
time with O(n)
space for the map and the result.
1 | /** |
1 | /** |
Topological Sorting
DFS: O(n)
time with O(n)
space for the map and the result.
1 | /** |
A method for solving a complex problem by breaking it down into a collection of simpler sub-problems.
Note: DP can’t return all results, it only returns max/min, yes/no or a certain value(like length, possible solutions, etc.).
Example: Number Triangle
We use an array f[i][j]
to record the minimum sum from (0, 0) to (i, j). For a certain (i, j), we know it is either from (i - 1, j - 1) or (i - 1, j). So the sub-problem is to traverse all elements in the triangle and calculate all the f[i][j] = min(f[i - 1][j - 1], f[i - 1][j]) + T[i]][j]
. The result will be the minimum one in f[n - 1][0, ..., n - 1]
.
We can also do it from bottom up. First we find the minimum of each pair in the last row, then we do above similarly but from bottom up. Finally the only one element left in the array is the result we are looking for.
A bonus point is doing this using only O(n)
extra space, where n is the total number of rows in the triangle. Since each time we calculate f[i][j]
, the result is only related with f[i - 1][]
, we can only use 1D array and update the elements in it in each for loop.
Here is the code from bottom up with O(n)
space cost.
1 | public class Solution { |
Dynamic Programming uses extra space to remember the mid-result. So that it is more efficient than recursive searching, which may repeated calculate the same mid-result for many times. In the above example, if we do a brute force search, it cost O(2^n)
time, where n is the height of the triangle (each row, we need to choose one from two, totally n rows). But it only costs O(n^2)
in theoretical(m rows, each row has n elements, totally n^2) or O(n)
, which means all numbers we only need to visit once.
When the head of the target list we want to return may be different from the original given list, we can use a dummy node linked to the result so that we can get what we want by calling dummy.next
.
Remove Duplicates from Sorted List II:
1 | public class Solution { |
1 | public class Solution { |
Similar Problems:
Partition List
Insertion Sort List
Reverse Nodes in k-group
Remove Node in Binary Search Tree
Copy List with Random Pointer
Swap Nodes in pairs
This method will cost O(n)
time with no extra space. The space is assigned by system(which could be O(1)
or O(h)
, h is the height of the tree).
1 | public ArrayList<Integer> preorderTraversal(TreeNode root) { |
Use a stack to store the nodes. Pay attention to the order of storing. We store the right subtree node before the left one, so that we can get the preorder nodes when pop from the stack.
This method will cost O(n)
time with O(h)
extra space for the stack.
1 | */ |
For a given sorted array (ascending order) and a target number, find the first index of this number in O(log n) time complexity.
If the target number does not exist in the array, return -1.
Example:
If the array is [1, 2, 3, 3, 4, 5, 10], for given target 3, return 2.
1 | class Solution { |
Key Points:
while(start < end)
: when start and end are overlapped each other, break. At this moment, we can return start or end. But it is different when we need to return the index before or after the target(see point 3).int mid = start + (end - start) / 2
: in order to avoid overflow of start + end
, we need to do a little trick here.while loop
: in the while loop, there are two different needs:1 | while(start <= end) { |
while(start <= end)
so that when we break the loop, two pointers are at two different(but neighbored) positions. Besides, if we want to return the one before the index, we return start
, otherwise we return end
.Update your browser to view this website correctly. Update my browser now