Skip to content

Commit bffd38c

Browse files
committed
chore: update 1877. Minimize Maximum Pair Sum in Array
1 parent 8009198 commit bffd38c

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: 1877. Minimize Maximum Pair Sum in Array
3+
description: he 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. 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. Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:Each element of nums is in exactly one pair, and The maximum pair sum is minimized. Return the minimized maximum pair sum after optimally pairing up the elements.
4+
keywords: [LeetCode, 1877. Minimize Maximum Pair Sum in Array, Medium, Math]
5+
tags: [
6+
LeetCode,
7+
Medium,
8+
Array
9+
Two Pointers
10+
Greedy
11+
Sorting,
12+
]
13+
---
14+
15+
# [{frontMatter.title}](https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/)
16+
17+
import Tabs from "@theme/Tabs";
18+
import TabItem from "@theme/TabItem";
19+
import CodeBlock from "@theme/CodeBlock";
20+
import DifficultyBadge from "@site/src/components/Badges/DifficultyBadge";
21+
import Solution from "!!raw-loader!./solution.js";
22+
import Description from "./_Description.md";
23+
import Examples from "./_Examples.md";
24+
25+
<DifficultyBadge difficulty="Medium" />
26+
27+
<Tabs>
28+
<TabItem value="description" label="題目描述" default>
29+
<details open>
30+
<summary>Description</summary>
31+
<Description />
32+
<Examples />
33+
</details>
34+
</TabItem>
35+
36+
<TabItem value="solution" label="解答">
37+
## Solution
38+
<CodeBlock language="js">{Solution}</CodeBlock>
39+
</TabItem>
40+
</Tabs>
41+
42+
## 解題思路
43+
44+
根據這題的 `Hint 1:Would sorting help find the optimal order?` 以及題目的 Topics 裡面包含 `Greedy` 以及 `Two Pointers`,得知解題策略為先把 `nums` 升冪排序,再使用貪婪演算法逐步檢視每次最大和最小值的 `Pair Sum`,而要找最大和最小則使用 `Two Pointers` 的做法。
45+
46+
因為 `nums.length` 一定會是偶數所以也不用考慮奇數狀況。
47+
每次算完 `Pair Sum` 後就更新目前的 `maxPairSum`,當 `two pointer` 結束後就是結果了
48+
49+
## 心得
50+
51+
讀懂題目比解題還難
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
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.
2+
3+
- 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.
4+
5+
Given an array nums of even length n, pair up the elements of nums into n / 2 pairs such that:
6+
7+
- Each element of nums is in exactly one pair, and
8+
- The maximum pair sum is minimized.
9+
10+
Return the minimized maximum pair sum after optimally pairing up the elements.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#### Example 1:
2+
3+
> **Input:** nums = [3,5,2,3]
4+
> **Output:** 7
5+
> **Explanation:**
6+
> The elements can be paired up into pairs (3,3) and (5,2).
7+
> The maximum pair sum is max(3+3, 5+2) = max(6, 7) = 7.
8+
9+
#### Example 2:
10+
11+
> **Input:** n = [3,5,4,2,4,6]
12+
> **Output:** 8
13+
> **Explanation:**
14+
> The elements can be paired up into pairs (3,5), (4,4), and (6,2).
15+
> The maximum pair sum is max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8.
16+
17+
## Constraints:
18+
19+
- `n == nums.length`
20+
- `2 <= n <= 105`
21+
- `n` is **even**.
22+
- `1 <= nums[i] <= 105`
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var minPairSum = function (nums) {
6+
nums.sort((a, b) => a - b);
7+
let left = 0;
8+
let right = nums.length - 1;
9+
let maxPairSum = 0;
10+
11+
while (left < right) {
12+
const pairSum = nums[left] + nums[right];
13+
maxPairSum = Math.max(maxPairSum, pairSum);
14+
left++;
15+
right--;
16+
}
17+
18+
return maxPairSum;
19+
};

0 commit comments

Comments
 (0)