Skip to content

Commit 618703a

Browse files
Chris WuChris Wu
Chris Wu
authored and
Chris Wu
committed
no message
1 parent 287d66f commit 618703a

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

3sum.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@ class Solution(object):
2424
We do not need to consider i after nums[i]>0, since sum of 3 positive will be always greater than zero. [7]
2525
We do not need to try the last two, since there are no rooms for l and r pointers.
2626
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'
2739
"""
2840

2941
def threeSum(self, nums):

network-delay-time.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@
1717
we use O(E) to make an adjacency list from edge list.
1818
we will go through the while loop V-1 times because we need to determined the distance of V-1 other nodes.
1919
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)
2626
E is the number of edges, which is the length of 'times' of the input
2727
V is the number of node, which is the 'N' of the inout
2828
2929
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.
3131
so, it is O(V+E).
3232
"""
3333
class Solution(object):

0 commit comments

Comments
 (0)