Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3183c0d

Browse files
feat: add solutions to lcof2 problem: No.061
1 parent 81e19c8 commit 3183c0d

File tree

5 files changed

+158
-2
lines changed

5 files changed

+158
-2
lines changed

‎lcof2/剑指 Offer II 061. 和最小的 k 个数对/README.md‎

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<p><strong>示例 3:</strong></p>
3434

3535
<pre>
36-
<strong>输入: </strong>nums1 = [1,2], nums2 = [3], k = 3
36+
<strong>输入: </strong>nums1 = [1,2], nums2 = [3], k = 3
3737
<strong>输出:</strong> [1,3],[2,3]
3838
<strong>解释: </strong>也可能序列中所有的数对都被返回:[1,3],[2,3]
3939
</pre>
@@ -58,22 +58,105 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61+
大顶堆
62+
6163
<!-- tabs:start -->
6264

6365
### **Python3**
6466

6567
<!-- 这里可写当前语言的特殊实现逻辑 -->
6668

6769
```python
68-
70+
class Solution:
71+
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
72+
hp = []
73+
for x in nums1[:k]:
74+
for y in nums2[:k]:
75+
heapq.heappush(hp, (-(x + y), [x, y]))
76+
if len(hp) > k:
77+
heapq.heappop(hp)
78+
return [p for _, p in hp]
6979
```
7080

7181
### **Java**
7282

7383
<!-- 这里可写当前语言的特殊实现逻辑 -->
7484

7585
```java
86+
class Solution {
87+
public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
88+
Queue<List<Integer>> pq = new PriorityQueue<>((p1, p2) -> {
89+
return p2.get(0) + p2.get(1) - (p1.get(0) + p1.get(1));
90+
});
91+
for (int i = 0; i < nums1.length && i < k; i++) {
92+
for (int j = 0; j < nums2.length && j < k; j++) {
93+
pq.offer(List.of(nums1[i], nums2[j]));
94+
if (pq.size() > k) {
95+
pq.poll();
96+
}
97+
}
98+
}
99+
return new ArrayList<>(pq);
100+
}
101+
}
102+
```
103+
104+
### **Go**
105+
106+
```go
107+
type pairHeap [][]int
108+
109+
func (a pairHeap) Len() int { return len(a) }
110+
func (a pairHeap) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
111+
func (a pairHeap) Less(i, j int) bool { return a[i][0]+a[i][1] > a[j][0]+a[j][1] }
112+
func (a *pairHeap) Push(x interface{}) { *a = append(*a, x.([]int)) }
113+
func (a *pairHeap) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp }
114+
115+
func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int {
116+
var hp pairHeap
117+
for _, x := range nums1[:min(k, len(nums1))] {
118+
for _, y := range nums2[:min(k, len(nums2))] {
119+
heap.Push(&hp, []int{x, y})
120+
if len(hp) > k {
121+
heap.Pop(&hp)
122+
}
123+
}
124+
}
125+
return hp
126+
}
127+
128+
func min(x, y int) int {
129+
if x < y {
130+
return x
131+
}
132+
return y
133+
}
134+
```
76135

136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
142+
using pii = pair<int, int>;
143+
auto cmp = [](pii p1, pii p2) { return p1.first + p1.second < p2.first + p2.second; };
144+
priority_queue<pii, vector<pii>, decltype(cmp)> pq(cmp);
145+
for (int i = 0; i < nums1.size() && i < k; ++i) {
146+
for (int j = 0; j < nums2.size() && j < k; ++j) {
147+
pq.push({nums1[i], nums2[j]});
148+
if (pq.size() > k) pq.pop();
149+
}
150+
}
151+
vector<vector<int>> ans;
152+
while (!pq.empty()) {
153+
pii p = pq.top();
154+
pq.pop();
155+
ans.push_back({p.first, p.second});
156+
}
157+
return ans;
158+
}
159+
};
77160
```
78161
79162
### **...**
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> kSmallestPairs(vector<int>& nums1, vector<int>& nums2, int k) {
4+
using pii = pair<int, int>;
5+
auto cmp = [](pii p1, pii p2) { return p1.first + p1.second < p2.first + p2.second; };
6+
priority_queue<pii, vector<pii>, decltype(cmp)> pq(cmp);
7+
for (int i = 0; i < nums1.size() && i < k; ++i) {
8+
for (int j = 0; j < nums2.size() && j < k; ++j) {
9+
pq.push({nums1[i], nums2[j]});
10+
if (pq.size() > k) pq.pop();
11+
}
12+
}
13+
vector<vector<int>> ans;
14+
while (!pq.empty()) {
15+
pii p = pq.top();
16+
pq.pop();
17+
ans.push_back({p.first, p.second});
18+
}
19+
return ans;
20+
}
21+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
type pairHeap [][]int
2+
3+
func (a pairHeap) Len() int { return len(a) }
4+
func (a pairHeap) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
5+
func (a pairHeap) Less(i, j int) bool { return a[i][0]+a[i][1] > a[j][0]+a[j][1] }
6+
func (a *pairHeap) Push(x interface{}) { *a = append(*a, x.([]int)) }
7+
func (a *pairHeap) Pop() interface{} { l := len(*a); tmp := (*a)[l-1]; *a = (*a)[:l-1]; return tmp }
8+
9+
func kSmallestPairs(nums1 []int, nums2 []int, k int) [][]int {
10+
var hp pairHeap
11+
for _, x := range nums1[:min(k, len(nums1))] {
12+
for _, y := range nums2[:min(k, len(nums2))] {
13+
heap.Push(&hp, []int{x, y})
14+
if len(hp) > k {
15+
heap.Pop(&hp)
16+
}
17+
}
18+
}
19+
return hp
20+
}
21+
22+
func min(x, y int) int {
23+
if x < y {
24+
return x
25+
}
26+
return y
27+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {
3+
Queue<List<Integer>> pq = new PriorityQueue<>((p1, p2) -> {
4+
return p2.get(0) + p2.get(1) - (p1.get(0) + p1.get(1));
5+
});
6+
for (int i = 0; i < nums1.length && i < k; i++) {
7+
for (int j = 0; j < nums2.length && j < k; j++) {
8+
pq.offer(List.of(nums1[i], nums2[j]));
9+
if (pq.size() > k) {
10+
pq.poll();
11+
}
12+
}
13+
}
14+
return new ArrayList<>(pq);
15+
}
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def kSmallestPairs(self, nums1: List[int], nums2: List[int], k: int) -> List[List[int]]:
3+
hp = []
4+
for x in nums1[:k]:
5+
for y in nums2[:k]:
6+
heapq.heappush(hp, (-(x + y), [x, y]))
7+
if len(hp) > k:
8+
heapq.heappop(hp)
9+
return [p for _, p in hp]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /