My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
cheapest-flights-within-k-stops(medium)
Shortest path problem with an additional dimension to denote steps: dis[i][j] means shortest path from src to i, passing j nodes. It is an O(mk) solution, where m is number of edges.
Another brilliant way is floyd + fast power. However, it takes O(n^3logk) time, which is much larger than former solution.
Time complexity: O(n^3logk) or O(mk).
Space complexity: O(n^2) or O(nk).
continuous-subarray-sum(easy)
Because the subarray should contains at least two elements, we should apply delay insertion to map.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
4sum(medium)
We can apply the same method of enumerating the first 2 positions as 3sum to this problem and the time complexity will be O(n^3).
Sum of four numbers is the sum of two pairs. Thus, another way is to use a map to save the sums of all pairs of numbers and use a set to store corresponding pairs. To avoid duplications, we can stipulate that numbers in the first pair should not greater than numbers in the second pair.
And to avoid cases like [-1, -1, -1, -1], sum=4, we should check if the combination of two pairs is available.
Time complexity: O(n^2logn) if using set, O(n^2) if using unordered_set(need to customize hash function).
Space complexity: O(n^2logn) if using set.
all-oone-data-structure(hard)
I have mentioned that: inc/dec by 1 --> linked list before.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
3sum-with-multiplicity(easy)
Similar to 3sum problem. Use map to count the multiplicity.
Time complexity: O(n^2).
Space complexity: O(100).
circular-array-loop(medium)
Bad description. [-2, 1, -1, -2, -2] returns false.
To be a loop, it must never change direction. In other words, the elements of the loop must all have the same sign, either all positive or all negative. If it reverses direction it is an oscillation rather than a loop.
Solution in O(1) space:
Mark nums[x] as (sign * size) when visiting.
Mark nums[x] as (2 * sign * size) after visiting.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
add-digits(easy)
Time complexity: It is hard to tell, I think it should be O(num_of_digits).
Space complexity: O(1).
add-two-numbers-ii(medium)
It is easy if we can modify the structure of the linked lists: we can reverse and add these two linked lists and then reverse back.
Without changing the structure, we should first align these two linked lists and then concatenate the remaining part. A trick is to ignore the carry numbers when adding two linked list and to handle carry numbers after concatenation.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
backspace-string-compare(easy)
Stack.
binary-tree-right-side-view(medium)
DFS. First, compute the depth of all subtrees. Second, for each subtree, decide which layers of its left subtree and its right subtree should be displayed.
clone-graph(easy)
It will be easy if we can use map to check if we have visited a node.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
exclusive-time-of-functions(medium)
DFS of partitions. Be clear about the return value you defined.
flatten-nested-list-iterator(hard)
Empty nested array makes it much more complicated because you could find no value when you go into the deeper dimension and then you should go back to shallower dimension.
Another key point is that NestedInteger and vector<NestedInteger> can mess us up.
k-empty-slots(medium)
It is quite a typical problem of reverse thinking.
We cannot find and insert a node to a linked list in O(1) time using common thinking. However, use reverse thinking, we can do it.
multiply-strings(medium)
High-precision multiplication. Do it as how we calculate the multiplication manually.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
3sum-closest(medium)
The same as the two pointers method of 3-sum.
add-binary(easy)
Same mechanism as high-precision calculation.
basic-calculator(hard)
It is a quite typical problem using two stacks. However, each time I tried to solve this problem, I cannot get it accepted at my first submission.
A trick to simplify the implementation is to define the priority of operators.
Another trick is how to handle negative numbers. It can be simplify our code if we combine the - with the following number rather than regard it as a operator.
My code can be viewed in my github: Proverbs Github.
The following difficulty is defined by myself.
integer-to-english-words(medium)
Divide the number into different parts according to the commas of writing a number.
The most difficult part of the problem is various corner cases, such as when should we output Zero.
alien-dictionary(medium)
Most sort algorithms are based on comparison, so we can exploit lexicographical order by comparing two contiguous words. Then we can represent the relationship using graph and use topological sort to check if there is a loop in the graph.
jewels-and-stones(easy)
license-key-formatting(easy)
meeting-rooms-ii(medium)
As usual, sort the intervals by the start time. Use current rooms as possible as we can. When there is no room available, we should add a new room.
So, the problem is how to judge if there is an available room. At first, I tried to use set and binary search. Then, add current interval to the searched room. Sure it is correct. However, we can find the room which ends earliest and add current interval to this room. It is corret because the following intervals start later then current interval and all available rooms for current interval are also available for the following intervals. Thus, we can add current interval to any one of the
available rooms.