Skip to content

Commit 642e40c

Browse files
committed
1584-min-cost-to-connect-all-points.md Reworded for heap sort.
1 parent 338346e commit 642e40c

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

solutions/1001-2000/1584-min-cost-to-connect-all-points-2.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1584. Min Cost to Connect All Points LeetCode Solution (Kruskal's Algorithm)
1+
# 1584. Min Cost to Connect All Points - LeetCode Solution (Kruskal's Algorithm)
22
LeetCode problem link: [1584. Min Cost to Connect All Points](https://leetcode.com/problems/min-cost-to-connect-all-points)
33

44
## LeetCode problem description

solutions/1001-2000/1584-min-cost-to-connect-all-points.md

+47-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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)
22
LeetCode problem link: [1584. Min Cost to Connect All Points](https://leetcode.com/problems/min-cost-to-connect-all-points)
33

44
## LeetCode problem description
@@ -101,7 +101,8 @@ Let us use the _common 5 steps_ to solve a _dynamic programming problem_.
101101
5. Check the `min_distances` array's value
102102
* Print the `min_distances` to see if it is as expected.
103103

104-
### The process of coding
104+
### Solution 1: Not use 'heap sort'
105+
#### The process of coding
105106
* Initialize `min_distances` and do the first iteration.
106107
```python
107108
min_distances = [float('inf')] * len(points) # This is just the `dp` array
@@ -176,6 +177,10 @@ for i, point in enumerate(points):
176177

177178
* Return `sum(min_distances)`.
178179

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+
179184
## Complexity
180185
`n` is the `points.length`.
181186
* Time: `O(n * n)`.
@@ -195,7 +200,7 @@ class Solution:
195200
while current_index is not None:
196201
visited[current_index] = True
197202
next_index = None
198-
min_distance = float('inf')
203+
minimum_distance = float('inf')
199204

200205
for i, point in enumerate(points):
201206
if visited[i]:
@@ -207,19 +212,16 @@ class Solution:
207212
abs(point[1] - points[current_index][1])
208213
)
209214

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]
212217
next_index = i
213218

214219
current_index = next_index
215220

216221
return sum(min_distances)
217222
```
218223

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)
223225
```python
224226
class Solution:
225227
def minCostConnectPoints(self, points: List[List[int]]) -> int:
@@ -249,6 +251,7 @@ class Solution:
249251
return result
250252
```
251253

254+
### Solution 1: Not use 'heap sort'
252255
## Java
253256
```java
254257
class Solution {
@@ -290,6 +293,12 @@ class Solution {
290293
}
291294
```
292295

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'
293302
## C++
294303
```cpp
295304
class Solution {
@@ -331,6 +340,12 @@ public:
331340
};
332341
```
333342
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'
334349
## JavaScript
335350
```javascript
336351
var minCostConnectPoints = function (points) {
@@ -369,6 +384,12 @@ var minCostConnectPoints = function (points) {
369384
};
370385
```
371386

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'
372393
## C#
373394
```c#
374395
public class Solution
@@ -416,6 +437,12 @@ public class Solution
416437
}
417438
```
418439

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'
419446
## Go
420447
```go
421448
func minCostConnectPoints(points [][]int) int {
@@ -461,6 +488,12 @@ func minCostConnectPoints(points [][]int) int {
461488
}
462489
```
463490

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'
464497
## Ruby
465498
```ruby
466499
def min_cost_connect_points(points)
@@ -497,6 +530,11 @@ def min_cost_connect_points(points)
497530
end
498531
```
499532

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+
500538
## C
501539
```c
502540
// Welcome to create a PR to complete the code of this language, thanks!

0 commit comments

Comments
 (0)