Description
https://leetcode.com/problems/clone-graph/description/
Solution
1 | /** |
Why don't you come to your senses?
https://leetcode.com/problems/clone-graph/description/
1 | /** |
https://leetcode.com/problems/copy-list-with-random-pointer/description/
The key point is to mantain a mapping from old node to copied node,
Step1: traverse the list to form the linked list
Step2: traverse the list to complete the random point relationship(using map)
1 | /** |
https://leetcode.com/problems/jump-game-ii/description/
1 | class Solution { |
We maintain the range of each depth, using leftBound
and rightBound
.
1 | class Solution { |
https://leetcode.com/problems/jump-game/description/
Traverse from the start node, mark its range as true, then we can check the end of the list when traversion finished.
1 | class Solution { |
In the solution above, we found some redundant traversion in the nested loop, to improve it, we should skip the meaningless procedure. To clarify how it works, we should introduce a pre-rule discovered by the fact:
Then we use a varialbe rightIndex
to record the reachable boundary in the right.
1 | class Solution { |
In the solution above, the procedure need to traverse all node regardless of the dataset. In fact we can terminate the result once we reached the end item of liste(success), or the current index is bigger than the rightIndex
(fail)
1 | class Solution { |
https://leetcode.com/problems/task-scheduler/description/
The key of this problem is how to manipulate the order. First we should collect the count of each character.
Then we can image the box of n + 1 empty position which need to be filled by the given character. The solution chain should be contained of several box linked together(except for the last box).Each time when we are filling one box. We will fetch the character based on the count of the character in the priority queue.
In last iteration, there is no need to fill the n + 1 slots.
1 | import java.util.PriorityQueue; |
https://leetcode.com/problems/gas-station/description/
1 | class Solution { |
1 | class Solution { |
https://leetcode.com/problems/odd-even-linked-list/description/
1 | /** |
https://leetcode.com/problems/longest-increasing-path-in-a-matrix/description/
1 | class Solution { |
https://leetcode.com/problems/find-peak-element/description/
1 | class Solution { |
1 | class Solution { |
https://leetcode.com/problems/fraction-to-recurring-decimal/description/
Pay attention to the fact that the pair of quote plus redundant in each divide iteration is the feature to judge whether the loop happened. Based on which, We ssing a list to record the order of the pair list, and maintain a set to judge whether the pair is occupied in each iteration. Once it was in the set, we stop it. Then we traverse the pair list to find the loop part and the unloop part.
1 | class Pair { |
hashCode
and equals
methods in the class.