Skip to content

Commit a43125d

Browse files
committed
Add solution 1877
1 parent 35530a4 commit a43125d

23 files changed

+669
-452
lines changed

README.md

Lines changed: 292 additions & 283 deletions
Large diffs are not rendered by default.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question1877 struct {
9+
para1877
10+
ans1877
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para1877 struct {
16+
nums []int
17+
}
18+
19+
// ans 是答案
20+
// one 代表第一个答案
21+
type ans1877 struct {
22+
one int
23+
}
24+
25+
func Test_Problem1877(t *testing.T) {
26+
27+
qs := []question1877{
28+
29+
{
30+
para1877{[]int{2, 2, 1, 2, 1}},
31+
ans1877{3},
32+
},
33+
34+
{
35+
para1877{[]int{100, 1, 1000}},
36+
ans1877{1001},
37+
},
38+
39+
{
40+
para1877{[]int{1, 2, 3, 4, 5}},
41+
ans1877{6},
42+
},
43+
44+
{
45+
para1877{[]int{3, 5, 2, 3}},
46+
ans1877{7},
47+
},
48+
49+
{
50+
para1877{[]int{3, 5, 4, 2, 4, 6}},
51+
ans1877{8},
52+
},
53+
}
54+
55+
fmt.Printf("------------------------Leetcode Problem 1877------------------------\n")
56+
57+
for _, q := range qs {
58+
_, p := q.ans1877, q.para1877
59+
fmt.Printf("【input】:%v 【output】:%v\n", p, minPairSum(p.nums))
60+
}
61+
fmt.Printf("\n\n\n")
62+
}

leetcode/1877.Minimize-Maximum-Pair-Sum-in-Array/1877. Minimize Maximum Pair Sum in Arrayz_test.go

Whitespace-only changes.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# [1877. Minimize Maximum Pair Sum in Array](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/)
2+
3+
4+
## 题目
5+
6+
The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
7+
8+
- For example, if we have pairs `(1,5)``(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
9+
10+
Given an array `nums` of **even** length `n`, pair up the elements of `nums` into `n / 2` pairs such that:
11+
12+
- Each element of `nums` is in **exactly one** pair, and
13+
- The **maximum pair sum** is **minimized**.
14+
15+
Return *the minimized **maximum pair sum** after optimally pairing up the elements*.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: nums = [3,5,2,3]
21+
Output: 7
22+
Explanation: The elements can be paired up into pairs (3,3) and (5,2).
23+
The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: nums = [3,5,4,2,4,6]
30+
Output: 8
31+
Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
32+
The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
33+
```
34+
35+
**Constraints:**
36+
37+
- `n == nums.length`
38+
- `2 <= n <= 105`
39+
- `n` is **even**.
40+
- `1 <= nums[i] <= 105`
41+
42+
## 题目大意
43+
44+
一个数对 (a,b) 的 **数对和** 等于 a + b 。**最大数对和** 是一个数对数组中最大的 数对和 。
45+
46+
- 比方说,如果我们有数对 (1,5) ,(2,3) 和 (4,4),**最大数对和** 为 max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8 。
47+
48+
给你一个长度为 **偶数** n 的数组 nums ,请你将 nums 中的元素分成 n / 2 个数对,使得:
49+
50+
- nums 中每个元素 **恰好** 在 一个 数对中,且
51+
- **最大数对和** 的值 **最小** 。
52+
53+
请你在最优数对划分的方案下,返回最小的 最大数对和 。
54+
55+
## 解题思路
56+
57+
- 要想最大数对和最小,那么最大的元素一定只能和最小的元素组合在一起,不然一定不是最小。当最大元素和最小元素组合在一起了,剩下的次最大元素也应该和次最小元素组合在一起。按照这个思路,先将数组从小到大排序,然后依次取出首尾元素,两两组合在一起。输出这些数对的最大值即为所求。
58+
59+
## 代码
60+
61+
```go
62+
package leetcode
63+
64+
import "sort"
65+
66+
func minPairSum(nums []int) int {
67+
sort.Ints(nums)
68+
n, res := len(nums), 0
69+
for i, val := range nums[:n/2] {
70+
res = max(res, val+nums[n-1-i])
71+
}
72+
return res
73+
}
74+
75+
func max(a, b int) int {
76+
if a > b {
77+
return a
78+
}
79+
return b
80+
}
81+
```

website/content/ChapterFour/1800~1899/1877.Minimize-Maximum-Pair-Sum-in-Array.md

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,82 @@
33

44
## 题目
55

6+
The **pair sum** of a pair `(a,b)` is equal to `a + b`. The **maximum pair sum** is the largest **pair sum** in a list of pairs.
7+
8+
- For example, if we have pairs `(1,5)``(2,3)`, and `(4,4)`, the **maximum pair sum** would be `max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8`.
9+
10+
Given an array `nums` of **even** length `n`, pair up the elements of `nums` into `n / 2` pairs such that:
11+
12+
- Each element of `nums` is in **exactly one** pair, and
13+
- The **maximum pair sum** is **minimized**.
14+
15+
Return *the minimized **maximum pair sum** after optimally pairing up the elements*.
16+
17+
**Example 1:**
18+
19+
```
20+
Input: nums = [3,5,2,3]
21+
Output: 7
22+
Explanation: The elements can be paired up into pairs (3,3) and (5,2).
23+
The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
24+
```
25+
26+
**Example 2:**
27+
28+
```
29+
Input: nums = [3,5,4,2,4,6]
30+
Output: 8
31+
Explanation: The elements can be paired up into pairs (3,5), (4,4), and (6,2).
32+
The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
33+
```
34+
35+
**Constraints:**
36+
37+
- `n == nums.length`
38+
- `2 <= n <= 105`
39+
- `n` is **even**.
40+
- `1 <= nums[i] <= 105`
41+
642
## 题目大意
743

44+
一个数对 (a,b) 的 **数对和** 等于 a + b 。**最大数对和** 是一个数对数组中最大的 数对和 。
45+
46+
- 比方说,如果我们有数对 (1,5) ,(2,3) 和 (4,4),**最大数对和** 为 max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8 。
47+
48+
给你一个长度为 **偶数** n 的数组 nums ,请你将 nums 中的元素分成 n / 2 个数对,使得:
49+
50+
- nums 中每个元素 **恰好** 在 一个 数对中,且
51+
- **最大数对和** 的值 **最小** 。
52+
53+
请你在最优数对划分的方案下,返回最小的 最大数对和 。
54+
855
## 解题思路
956

57+
- 要想最大数对和最小,那么最大的元素一定只能和最小的元素组合在一起,不然一定不是最小。当最大元素和最小元素组合在一起了,剩下的次最大元素也应该和次最小元素组合在一起。按照这个思路,先将数组从小到大排序,然后依次取出首尾元素,两两组合在一起。输出这些数对的最大值即为所求。
1058

1159
## 代码
1260

1361
```go
62+
package leetcode
1463

15-
```
64+
import "sort"
1665

66+
func minPairSum(nums []int) int {
67+
sort.Ints(nums)
68+
n, res := len(nums), 0
69+
for i, val := range nums[:n/2] {
70+
res = max(res, val+nums[n-1-i])
71+
}
72+
return res
73+
}
74+
75+
func max(a, b int) int {
76+
if a > b {
77+
return a
78+
}
79+
return b
80+
}
81+
```
1782

1883

1984
----------------------------------------------

0 commit comments

Comments
 (0)