-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
373. Find K Pairs with Smallest Sums.go
67 lines (60 loc) · 1.44 KB
/
373. Find K Pairs with Smallest Sums.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
65
66
67
package leetcode
import (
"container/heap"
"sort"
)
// 解法一 优先队列
func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int {
result, h := [][]int{}, &minHeap{}
if len(nums1) == 0 || len(nums2) == 0 || k == 0 {
return result
}
if len(nums1)*len(nums2) < k {
k = len(nums1) * len(nums2)
}
heap.Init(h)
for _, num := range nums1 {
heap.Push(h, []int{num, nums2[0], 0})
}
for len(result) < k {
min := heap.Pop(h).([]int)
result = append(result, min[:2])
if min[2] < len(nums2)-1 {
heap.Push(h, []int{min[0], nums2[min[2]+1], min[2] + 1})
}
}
return result
}
type minHeap [][]int
func (h minHeap) Len() int { return len(h) }
func (h minHeap) Less(i, j int) bool { return h[i][0]+h[i][1] < h[j][0]+h[j][1] }
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.([]int))
}
func (h *minHeap) Pop() interface{} {
old := *h
n := len(old)
x := old[n-1]
*h = old[0 : n-1]
return x
}
// 解法二 暴力解法
func kSmallestPairs1(nums1 []int, nums2 []int, k int) [][]int {
size1, size2, res := len(nums1), len(nums2), [][]int{}
if size1 == 0 || size2 == 0 || k < 0 {
return nil
}
for i := 0; i < size1; i++ {
for j := 0; j < size2; j++ {
res = append(res, []int{nums1[i], nums2[j]})
}
}
sort.Slice(res, func(i, j int) bool {
return res[i][0]+res[i][1] < res[j][0]+res[j][1]
})
if len(res) >= k {
return res[:k]
}
return res
}