LeetCode 561. 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.
Note:
n is a positive integer, which is in the range of [1, 10000].
All the integers in the array will be in the range of [-10000, 10000].
思路:
就是给2n个数分组,两两一组,使用所有组中小的那个数加起来和最小。
那么就是先将所有数排序,然后就是以排序后的值2个一组分组,然后取的就是两个中前一个的索引对应的值。
代码:
class Solution {
public:int arrayPairSum(vector<int>& nums) {vector<int>::size_type nsize=nums.size();//计算数组大小if(nsize==0){//如果大小为0,直接返回0return 0;}sort(nums.begin(),nums.end());//排序int sum=0;for(int i=0;i<nsize;i+=2){//计算两两一组中,数小的那个,和即为结果sum+=nums[i];}return sum;}
};
输出结果: 106ms
LeetCode 561. Array Partition I相关推荐
- [LeetCode]561. Array Partition I (数组分区 1)
561. Array Partition I Given an array of 2n integers, your task is to group these integers into n pa ...
- LeetCode 561 Array Partition I(数组划分)
翻译 原文 Given an array of 2n integers, your task is to group these integers into n pairs of integer, s ...
- leetcode#561 Array Partition I
Description: Given an array of 2n integers, your task is to group these integers into n pairs of int ...
- Leetcode PHP题解--D14 561. Array Partition I
561. Array Partition I 题目链接 561. Array Partition I 题目分析 本题给了一个数组,要求将数组分为n个只有2个元素的一对. 使得每对数字中最小的数加起来的 ...
- Leetcode刷题记录[java]——561 Array Partition I
一.前言 二.题561 Array Partition I Given an array of 2n integers, your task is to group these integers in ...
- 561.Array Partition I--Python
刚开始学习Python编程,欢迎交流学习! 561.Array Partition I Given an array of 2n integers, your task is to group the ...
- 【刷leetcode,拿Offer-009】561. Array Partition I(贪心,C++)
题目链接 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...
- 561. Array Partition I
原题 Given an array of 2n integers, your task is to group these integers into n pairs of integer, say ...
- 561. 数组拆分 I(javascript)561. Array Partition I
给定长度为 2n 的整数数组 nums ,你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), -, (an, bn) ,使得从 1 到 n 的 min(ai, bi) 总和 ...
最新文章
- Kubernetes 1.14 版本发布:正式支持Windows 节点,持久化本地卷进入GA
- html5 手机上传图片
- Python之max(num, key=lambda x:x[0])用法的详细解析
- Java的不同版本:J2SE、J2EE、J2ME的区别
- 牛人写SCI常用经典词和常用句型
- 【最简便解法】1066 图像过滤 (15分)
- 【JAVA编码专题】JAVA字符编码系列一:Unicode,GBK,GB2312,UTF-8概念基础
- java byte转换成string_Java byte []到/从String转换
- 动态区间第k小:树状数组套权值线段树
- c++11并发指南系列
- 除了HTML+CSS,成就高薪web前端还需要学习什么技术?
- MATLAB命令窗常用命令
- 虚拟机桥接模式配置网络
- deep_sort文章阅读(一)yolo的训练:PART1: 安装opencv(为了make时opencv=1)
- ASP.NET的优点
- IC设计数字工程师技能必备
- VC++6.0 用gSoap客户端访问WebService
- 数学建模写作时相关的套路
- 云计算基础技术及解决方案介绍 - ZCCT考试
- c语言定义max和命令,C语言#define定义函数
热门文章
- python函数执行shell_6方法,python中执行shell命令
- python io密集型应用案例-Python中单线程、多线程和多进程的效率对比实验实例
- 学习笔记3 :pyqt5 显示opencv 和 PIL图片
- 学习笔记1:更改python下载源
- Bootstrap-CSS-按钮-图片-辅助类-响应式
- linux目标机无法连接到网络,linux 安装了xunsearch服务,但是PHP连接显示:目标计算机积极拒绝...
- 超好看的动态流量卡官网源码多功能集成式源码
- Bubble——Typecho 极简风格响应式主题
- 百度SEO站群404二次元时间倒计时页面源码
- WordPress 高颜值自适应黑/白模式Puock主题