-
Notifications
You must be signed in to change notification settings - Fork 13
/
solution.go
64 lines (52 loc) · 1.61 KB
/
solution.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import (
"container/heap"
"math"
)
type Element struct {
value, row, col int
}
type MinHeap []Element
func (h MinHeap) Len() int { return len(h) }
func (h MinHeap) Less(i, j int) bool { return h[i].value < h[j].value }
func (h MinHeap) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
func (h *MinHeap) Push(x interface{}) {
*h = append(*h, x.(Element))
}
func (h *MinHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
func smallestRange(nums [][]int) []int {
minHeap := &MinHeap{}
maxValue := math.MinInt32
// Initialize heap with first element from each list
for i := range nums {
heap.Push(minHeap, Element{nums[i][0], i, 0})
if nums[i][0] > maxValue {
maxValue = nums[i][0]
}
}
rangeStart, rangeEnd := 0, math.MaxInt32
for minHeap.Len() > 0 {
minElement := heap.Pop(minHeap).(Element)
// Update the smallest range
if maxValue - minElement.value < rangeEnd - rangeStart {
rangeStart = minElement.value
rangeEnd = maxValue
}
// Move to the next element in the current list
if minElement.col + 1 < len(nums[minElement.row]) {
nextValue := nums[minElement.row][minElement.col + 1]
heap.Push(minHeap, Element{nextValue, minElement.row, minElement.col + 1})
if nextValue > maxValue {
maxValue = nextValue
}
} else {
break // One list is exhausted
}
}
return []int{rangeStart, rangeEnd}
}