Skip to content

Commit

Permalink
add: my solution for kpairswithlargestsums
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiwei-Feng committed Jun 2, 2021
1 parent a76a367 commit 3b2f654
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Pattern14 - K-way merge/K_Pairs_with_Largest_Sums/solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package kpairswithlargestsums

import "container/heap"

/*
* K Pairs with Largest Sums
Given two sorted arrays in descending order, find ‘K’ pairs with the largest sum where each pair consists of numbers from both the arrays.
Input: L1=[9, 8, 2], L2=[6, 3, 1], K=3
Output: [9, 3], [9, 6], [8, 6]
Explanation: These 3 pairs have the largest sum. No other pair has a sum larger than any of these.
Input: L1=[5, 2, 1], L2=[2, -1], K=3
Output: [5, 2], [5, -1], [2, 2]
ref: https://leetcode-cn.com/problems/find-k-pairs-with-smallest-sums/
*/

func kLargestPairs(nums1, nums2 []int, k int) [][]int {
var (
mheap MinHeap
)
heap.Init(&mheap)
for _, v1 := range nums1 {
for _, v2 := range nums2 {
if mheap.Len() < k {
heap.Push(&mheap, []int{v1, v2})
} else if v1+v2 > mheap[0][0]+mheap[0][1] {
heap.Pop(&mheap)
heap.Push(&mheap, []int{v1, v2})
} else {
break
}
}
}

return mheap
}

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[:n-1]
return x
}
28 changes: 28 additions & 0 deletions Pattern14 - K-way merge/K_Pairs_with_Largest_Sums/solution_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package kpairswithlargestsums

import (
"reflect"
"testing"
)

func Test_kLargestPairs(t *testing.T) {
type args struct {
nums1 []int
nums2 []int
k int
}
tests := []struct {
name string
args args
want [][]int
}{
{"case1", args{[]int{9, 8, 2}, []int{6, 3, 1}, 3}, [][]int{{9, 3}, {9, 6}, {8, 6}}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := kLargestPairs(tt.args.nums1, tt.args.nums2, tt.args.k); !reflect.DeepEqual(got, tt.want) {
t.Errorf("kLargestPairs() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 3b2f654

Please sign in to comment.