1
- # 1584. Min Cost to Connect All Points' LeetCode Solution (Prim's Algorithm)
1
+ # 1584. Min Cost to Connect All Points - LeetCode Solution (Prim's Algorithm)
2
2
LeetCode problem link: [ 1584. Min Cost to Connect All Points] ( https://leetcode.com/problems/min-cost-to-connect-all-points )
3
3
4
4
## LeetCode problem description
@@ -101,7 +101,8 @@ Let us use the _common 5 steps_ to solve a _dynamic programming problem_.
101
101
5 . Check the ` min_distances ` array's value
102
102
* Print the ` min_distances ` to see if it is as expected.
103
103
104
- ### The process of coding
104
+ ### Solution 1: Not use 'heap sort'
105
+ #### The process of coding
105
106
* Initialize ` min_distances ` and do the first iteration.
106
107
``` python
107
108
min_distances = [float (' inf' )] * len (points) # This is just the `dp` array
@@ -176,6 +177,10 @@ for i, point in enumerate(points):
176
177
177
178
* Return ` sum(min_distances) ` .
178
179
180
+ ### Solution 2: Use 'heap sort' (recommended)
181
+ * If you use ** heap sort** , ` current_index ` , ` next_index ` , ` minimum_distance ` is not needed, because _ heap sort_ knows which is the minimum value.
182
+ * ` visited ` is also not needed, because each ` heappop() ` means that a point has been ` visited ` .
183
+
179
184
## Complexity
180
185
` n ` is the ` points.length ` .
181
186
* Time: ` O(n * n) ` .
@@ -195,7 +200,7 @@ class Solution:
195
200
while current_index is not None :
196
201
visited[current_index] = True
197
202
next_index = None
198
- min_distance = float (' inf' )
203
+ minimum_distance = float (' inf' )
199
204
200
205
for i, point in enumerate (points):
201
206
if visited[i]:
@@ -207,19 +212,16 @@ class Solution:
207
212
abs (point[1 ] - points[current_index][1 ])
208
213
)
209
214
210
- if min_distances[i] < min_distance :
211
- min_distance = min_distances[i]
215
+ if min_distances[i] < minimum_distance :
216
+ minimum_distance = min_distances[i]
212
217
next_index = i
213
218
214
219
current_index = next_index
215
220
216
221
return sum (min_distances)
217
222
```
218
223
219
- ### Solution 2: Use 'heap sort'
220
- * If you use ` heap sort ` , ` current_index ` , ` next_index ` is not needed, because heap sort knows which is the minimum value.
221
- * ` visited ` is also not needed, because each ` heappop() ` means that a point has been ` visited ` .
222
-
224
+ ### Solution 2: Use 'heap sort' (recommended)
223
225
``` python
224
226
class Solution :
225
227
def minCostConnectPoints (self , points : List[List[int ]]) -> int :
@@ -249,6 +251,7 @@ class Solution:
249
251
return result
250
252
```
251
253
254
+ ### Solution 1: Not use 'heap sort'
252
255
## Java
253
256
``` java
254
257
class Solution {
@@ -290,6 +293,12 @@ class Solution {
290
293
}
291
294
```
292
295
296
+ ### Solution 2: Use 'heap sort' (recommended)
297
+ ``` java
298
+ // Welcome to create a PR to complete the code of this language, thanks!
299
+ ```
300
+
301
+ ### Solution 1: Not use 'heap sort'
293
302
## C++
294
303
``` cpp
295
304
class Solution {
@@ -331,6 +340,12 @@ public:
331
340
};
332
341
```
333
342
343
+ ### Solution 2: Use 'heap sort' (recommended)
344
+ ```cpp
345
+ // Welcome to create a PR to complete the code of this language, thanks!
346
+ ```
347
+
348
+ ### Solution 1: Not use 'heap sort'
334
349
## JavaScript
335
350
``` javascript
336
351
var minCostConnectPoints = function (points ) {
@@ -369,6 +384,12 @@ var minCostConnectPoints = function (points) {
369
384
};
370
385
```
371
386
387
+ ### Solution 2: Use 'heap sort' (recommended)
388
+ ``` javascript
389
+ // Welcome to create a PR to complete the code of this language, thanks!
390
+ ```
391
+
392
+ ### Solution 1: Not use 'heap sort'
372
393
## C#
373
394
``` c#
374
395
public class Solution
@@ -416,6 +437,12 @@ public class Solution
416
437
}
417
438
```
418
439
440
+ ### Solution 2: Use 'heap sort' (recommended)
441
+ ``` c#
442
+ // Welcome to create a PR to complete the code of this language, thanks!
443
+ ```
444
+
445
+ ### Solution 1: Not use 'heap sort'
419
446
## Go
420
447
``` go
421
448
func minCostConnectPoints (points [][]int ) int {
@@ -461,6 +488,12 @@ func minCostConnectPoints(points [][]int) int {
461
488
}
462
489
```
463
490
491
+ ### Solution 2: Use 'heap sort' (recommended)
492
+ ``` go
493
+ // Welcome to create a PR to complete the code of this language, thanks!
494
+ ```
495
+
496
+ ### Solution 1: Not use 'heap sort'
464
497
## Ruby
465
498
``` ruby
466
499
def min_cost_connect_points (points )
@@ -497,6 +530,11 @@ def min_cost_connect_points(points)
497
530
end
498
531
```
499
532
533
+ ### Solution 2: Use 'heap sort' (recommended)
534
+ ``` ruby
535
+ # Welcome to create a PR to complete the code of this language, thanks!
536
+ ```
537
+
500
538
## C
501
539
``` c
502
540
// Welcome to create a PR to complete the code of this language, thanks!
0 commit comments