1877:数组中最大数对和的最小值

This commit is contained in:
huangge1199 2021-06-07 13:12:59 +08:00
parent 3421ec0a55
commit 8cd6e46ea9
2 changed files with 117 additions and 0 deletions

View File

@ -0,0 +1,73 @@
//一个数对 (a,b) 数对和 等于 a + b 最大数对和 是一个数对数组中最大的 数对和
//
//
// 比方说如果我们有数对 (1,5) (2,3) (4,4)最大数对和 max(1+5, 2+3, 4+4) = max(6, 5, 8) =
//8
//
//
// 给你一个长度为 偶数 n 的数组 nums 请你将 nums 中的元素分成 n / 2 个数对使得
//
//
// nums 中每个元素 恰好 一个 数对中
// 最大数对和 的值 最小
//
//
// 请你在最优数对划分的方案下返回最小的 最大数对和
//
//
//
// 示例 1
//
// 输入nums = [3,5,2,3]
//输出7
//解释数组中的元素可以分为数对 (3,3) (5,2)
//最大数对和为 max(3+3, 5+2) = max(6, 7) = 7
//
//
// 示例 2
//
// 输入nums = [3,5,4,2,4,6]
//输出8
//解释数组中的元素可以分为数对 (3,5)(4,4) (6,2)
//最大数对和为 max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8
//
//
//
//
// 提示
//
//
// n == nums.length
// 2 <= n <= 105
// n 偶数
// 1 <= nums[i] <= 105
//
// Related Topics 贪心算法 排序
// 👍 4 👎 0
package leetcode.editor.cn;
import java.util.Arrays;
//1877:数组中最大数对和的最小值
public class MinimizeMaximumPairSumInArray{
public static void main(String[] args) {
//测试代码
Solution solution = new MinimizeMaximumPairSumInArray().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int minPairSum(int[] nums) {
Arrays.sort(nums);
int max = Integer.MIN_VALUE;
int length = nums.length;
for (int i = 0; i < length / 2; i++) {
max = Math.max(nums[i] + nums[length - 1 - i], max);
}
return max;
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,44 @@
<p>一个数对 <code>(a,b)</code> 的 <strong>数对和</strong> 等于 <code>a + b</code> 。<strong>最大数对和</strong> 是一个数对数组中最大的 <strong>数对和</strong> 。</p>
<ul>
<li>比方说,如果我们有数对 <code>(1,5)</code> <code>(2,3)</code> 和 <code>(4,4)</code><strong>最大数对和</strong> 为 <code>max(1+5, 2+3, 4+4) = max(6, 5, 8) = 8</code> 。</li>
</ul>
<p>给你一个长度为 <strong>偶数</strong> <code>n</code> 的数组 <code>nums</code> ,请你将 <code>nums</code> 中的元素分成 <code>n / 2</code> 个数对,使得:</p>
<ul>
<li><code>nums</code> 中每个元素 <strong>恰好</strong> 在 <strong>一个</strong> 数对中,且</li>
<li><strong>最大数对和</strong> 的值 <strong>最小</strong> 。</li>
</ul>
<p>请你在最优数对划分的方案下,返回最小的 <strong>最大数对和</strong> 。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre><b>输入:</b>nums = [3,5,2,3]
<b>输出:</b>7
<b>解释:</b>数组中的元素可以分为数对 (3,3) 和 (5,2) 。
最大数对和为 max(3+3, 5+2) = max(6, 7) = 7 。
</pre>
<p><strong>示例 2</strong></p>
<pre><b>输入:</b>nums = [3,5,4,2,4,6]
<b>输出:</b>8
<b>解释:</b>数组中的元素可以分为数对 (3,5)(4,4) 和 (6,2) 。
最大数对和为 max(3+5, 4+4, 6+2) = max(8, 8, 8) = 8 。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>n == nums.length</code></li>
<li><code>2 &lt;= n &lt;= 10<sup>5</sup></code></li>
<li><code>n</code> 是 <strong>偶数</strong> 。</li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>
<div><div>Related Topics</div><div><li>贪心算法</li><li>排序</li></div></div>\n<div><li>👍 4</li><li>👎 0</li></div>