Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1937 from kkr2/feat/1584-min-cost-to-c…
Browse files Browse the repository at this point in the history
…onnect.go

Create 1584-min-cost-to-connect.go
  • Loading branch information
tahsintunan authored Jan 9, 2023
2 parents c78ef30 + 5a6b0a7 commit 542c2c8
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions go/1584-min-cost-to-connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
func minCostConnectPoints(points [][]int) int {

n := len(points)
adj := make([][][]int, n)
for i := 0; i < n; i++ {
x1, y1 := points[i][0], points[i][1]
for j := i + 1; j < n; j++ {
x2, y2 := points[j][0], points[j][1]
dist := abs(x1-x2) + abs(y1-y2)
adj[i] = append(adj[i], []int{dist, j})
adj[j] = append(adj[j], []int{dist, i})
}
}
// prims
res := 0
visited := make(map[int]bool)

h := &IntHeap{[2]int{0, 0}}

for len(visited) < n {
pntObj := heap.Pop(h).([2]int)
cost, pnt := pntObj[0], pntObj[1]

if visited[pnt] {
continue
}
res += cost
visited[pnt] = true

for _, neighbour := range adj[pnt] {
nCost, nPoint := neighbour[0], neighbour[1]

if !visited[nPoint] {
heap.Push(h, [2]int{nCost, nPoint})
}
}
}
return res
}

func abs(n int) int {
if n > 0 {
return n
}
return -n
}

type IntHeap [][2]int

func (h IntHeap) Len() int { return len(h) }
func (h IntHeap) Less(i, j int) bool { return h[i][0] < h[j][0] }
func (h IntHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }

func (h *IntHeap) Push(x interface{}) {
*h = append(*h, x.([2]int))
}

func (h *IntHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[:n-1]
return x
}

0 comments on commit 542c2c8

Please sign in to comment.