【LeetCode】11.Array and String —Array Partition I 数组分区

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), ..., (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

根据题意,想找到所有可能组合中最小数和的最大值。按照题目给出的例子,结合网上查找的数学证明,要按从小到大的顺序两两组合,再取最小的数则可使其和最大。所以我们先对数组从小到大排序 ,然后跳位相加即为所求。(感觉和two-pointer也没啥关系呀-.-)

 1 class Solution {
 2 public:
 3 int arrayPairSum(vector<int>&nums) {
 4     int result=0;
 5     //先把数组按从小到大的顺序进行排序
 6     sort(nums.begin(), nums.end());
 7     for (int i = 0; i < nums.size(); i=i+2) {
 8         result += nums[i];
 9     }
10     return result;
11 }
12 };

原文地址:https://www.cnblogs.com/hu-19941213/p/10977596.html

时间: 2024-07-30 11:24:16

【LeetCode】11.Array and String —Array Partition I 数组分区的相关文章

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

Leetcode | Search in Rotated Sorted Array I &amp; II

Search in Rotated Sorted Array I Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise re

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

leetcode 题解:Merge Sorted Array(两个已排序数组归并)

题目: Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements initialized in A and B ar

js基础篇string&amp;&amp;array(应YX同学面试复习要求 - -)

js中的数据类型一共有五个基本数据类型,分别是undefined,null,boolean,number,string. js中的Object类型中包括两大类型:number类型和array类型.而我们现在要说的就是string和array! string 1.length   可以取出字符串有多少个字符                                 "abc".length      结果:3 2.charAt    返回指定索引位置的字符 "abc&qu

LeetCode:Remove Duplicates from Sorted Array &amp;&amp; Remove Element

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. For example, Given input array A =

Array和String方法总结—实例

说明:每一部分总结后面有实例代码,代码中黄色框的方法不会改变原数组.                  代表array和string共有的方法             代表参数 Array --普通方法 栈:   pop()   push(多个项) 队列:shift()  unshift(多个项) 排序:sort([函数])  reverse() 转换:toString()  toLocateString()   join([分隔符]) 操作:concat([多个项])     slice(起点