File tree 2 files changed +19
-7
lines changed 2 files changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,18 @@ class Solution(object):
24
24
We do not need to consider i after nums[i]>0, since sum of 3 positive will be always greater than zero. [7]
25
25
We do not need to try the last two, since there are no rooms for l and r pointers.
26
26
You can think of it as The last two have been tried by all others. [8]
27
+
28
+ For time complexity
29
+ Sorting takes O(NlogN)
30
+ Now, we need to think as if the 'nums' is really really big
31
+ We iterate through the 'nums' once, and each time we iterate the whole array again by a while loop
32
+ So it is O(NlogN+N^2)~=O(N^2)
33
+
34
+ For space complexity
35
+ We didn't use extra space except the 'res'
36
+ Since we may store the whole 'nums' in it
37
+ So it is O(N)
38
+ N is the length of 'nums'
27
39
"""
28
40
29
41
def threeSum (self , nums ):
Original file line number Diff line number Diff line change 17
17
we use O(E) to make an adjacency list from edge list.
18
18
we will go through the while loop V-1 times because we need to determined the distance of V-1 other nodes.
19
19
for every loop
20
- we need O(logV ) to get the nearest node, bc we pop from priority queue .
21
- we go push the node the priority queue d times (which is the depth of every node)
22
- push the node takes O(logV ), we assume all the node is in the queue.
23
- so every loop we use O(d*logV+logV )~=O(d*logV )
24
- so total for total, it is O((V-1)*(d*logV ))+O(E), we know that V-1~=V and V*d=E
25
- so it is O(ElogV )+O(E)~=O(ElogV )
20
+ we pop from priority queue (here need O(logE ) to get the nearest node) .
21
+ we push the node's neighbor to the priority queue d times (which is the degree of the node)
22
+ push the node takes O(logE ), we assume all the node is in the queue.
23
+ so every loop we use O(d*logE+logE )~=O(d*logE )
24
+ so total for total, it is O((V-1)*(d*logE ))+O(E), we know that V-1~=V and V*d=E
25
+ so it is O(ElogE )+O(E)~=O(ElogE )
26
26
E is the number of edges, which is the length of 'times' of the input
27
27
V is the number of node, which is the 'N' of the inout
28
28
29
29
for space complexity
30
- we used O(V) in both the 'dis' and 'pq', and O(E) on the adjacency list.
30
+ we used O(V) and O(E) in 'dis' and 'pq', and O(E) on the adjacency list.
31
31
so, it is O(V+E).
32
32
"""
33
33
class Solution (object ):
You can’t perform that action at this time.
0 commit comments