You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Time Complexity: O(N^2 log(N)) where N is the length of points. N^2 comes from the fact we need to find the distance between a currNode and every other node to pick the shortest distance. log(N) comes from Priority Queue
3
4
// Space Complexity: O(N^2)
4
5
publicintminCostConnectPoints(int[][] points) {
5
-
PriorityQueue<int[]> pq = newPriorityQueue<>((a,b) -> a[0] - b[0]); // edge weight, the index of next node
6
-
pq.offer(newint[]{0,0});
6
+
PriorityQueue<int[]> pq = newPriorityQueue<>((a,b) -> a[0] - b[0]); // edge weight, the index of next node
7
+
pq.offer(newint[] { 0, 0});
7
8
intlen = points.length;
8
9
Set<Integer> visited = newHashSet<>();
9
10
intcost = 0;
10
-
11
-
// When visited.size() == points.len meaning that all the nodes has been connected.
12
-
while(visited.size() < len){
11
+
12
+
// When visited.size() == points.len meaning that all the nodes has been connected.
0 commit comments