Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
373_find_k_pairs_with_smallest_sums/find_k_pairs_with_smallest_sums.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,74 @@ | ||
package find_k_pairs_with_smallest_sums | ||
|
||
// https://leetcode.com/problems/find-k-pairs-with-smallest-sums | ||
|
||
// level: 2 | ||
|
||
//leetcode submit region begin(Prohibit modification and deletion) | ||
import "container/heap" | ||
|
||
type Item struct { | ||
num1 int | ||
num2 int | ||
p int | ||
} | ||
|
||
type PriorityQueue []*Item | ||
|
||
func (pq PriorityQueue) Len() int { | ||
return len(pq) | ||
} | ||
|
||
func (pq PriorityQueue) Less(i, j int) bool { | ||
return pq[i].p >= pq[j].p | ||
} | ||
|
||
func (pq PriorityQueue) Swap(i, j int) { | ||
pq[i], pq[j] = pq[j], pq[i] | ||
} | ||
|
||
func (pq *PriorityQueue) Push(x interface{}) { | ||
*pq = append(*pq, x.(*Item)) | ||
} | ||
|
||
func (pq *PriorityQueue) Pop() interface{} { | ||
old := *pq | ||
n := len(old) - 1 | ||
x := old[n] | ||
*pq = old[0:n] | ||
return x | ||
} | ||
|
||
func (pq PriorityQueue) Peek() Item { | ||
return *pq[0] | ||
} | ||
|
||
func (pq *PriorityQueue) Init(nums []*Item) { | ||
*pq = nums[:] | ||
heap.Init(pq) | ||
} | ||
|
||
func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int { | ||
h := &PriorityQueue{} | ||
for _, i := range nums1 { | ||
for _, j := range nums2 { | ||
if h.Len() < k { | ||
heap.Push(h, &Item{i, j, i + j}) | ||
} else if sum := i + j; sum < h.Peek().p { | ||
heap.Pop(h) | ||
heap.Push(h, &Item{i, j, sum}) | ||
} | ||
} | ||
} | ||
if h.Len() < k { | ||
k = h.Len() | ||
} | ||
ret := make([][]int, k) | ||
for i := k - 1; i >= 0; i-- { | ||
item := heap.Pop(h).(*Item) | ||
ret[i] = []int{item.num1, item.num2} | ||
} | ||
return ret | ||
} | ||
|
||
//leetcode submit region end(Prohibit modification and deletion) |
63 changes: 63 additions & 0 deletions
63
373_find_k_pairs_with_smallest_sums/find_k_pairs_with_smallest_sums_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,63 @@ | ||
package find_k_pairs_with_smallest_sums | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_kSmallestPairs(t *testing.T) { | ||
type args struct { | ||
nums1 []int | ||
nums2 []int | ||
k int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want [][]int | ||
}{ | ||
{ | ||
name: "test1", | ||
args: args{ | ||
[]int{1, 7, 11}, | ||
[]int{2, 4, 6}, | ||
3, | ||
}, | ||
want: [][]int{{1, 2}, {1, 4}, {1, 6}}, | ||
}, | ||
{ | ||
name: "test2", | ||
args: args{ | ||
[]int{1, 1, 2}, | ||
[]int{1, 2, 3}, | ||
2, | ||
}, | ||
want: [][]int{{1, 1}, {1, 1}}, | ||
}, | ||
{ | ||
name: "test3", | ||
args: args{ | ||
[]int{1, 2}, | ||
[]int{3}, | ||
3, | ||
}, | ||
want: [][]int{{1, 3}, {2, 3}}, | ||
}, | ||
{ | ||
name: "test4", | ||
args: args{ | ||
[]int{-13, 23, 44, 117, 900, 990}, | ||
[]int{-15, 20, 35, 118, 223, 500, 663, 717, 789, 813}, | ||
10, | ||
}, | ||
want: [][]int{{-13, -15}, {-13, 20}, {23, -15}, {-13, 35}, {44, -15}, {23, 20}, {23, 35}, {44, 20}, {44, 35}, {117, -15}}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := kSmallestPairs(tt.args.nums1, tt.args.nums2, tt.args.k); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("kSmallestPairs() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |