-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add: my solution for kpairswithlargestsums
- Loading branch information
1 parent
a76a367
commit 3b2f654
Showing
2 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
Pattern14 - K-way merge/K_Pairs_with_Largest_Sums/solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
Pattern14 - K-way merge/K_Pairs_with_Largest_Sums/solution_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |