LeetCode 561. Array Partition I (C++)
题目:
Given an array of 2n integers, your task is to group these integers into npairs 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:
- 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].
分析1:
给定一个大小为2n数组,其内元素两两一组,组内求min,总体求max值。
很明显,我们希望每一组内的两个元素尽可能的相近,这样在求min之后的和一定是最大的。所以可以进行排序,然后直接对0,2,4,...加和处理。
程序1:
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int sum = ;
sort(nums.begin(), nums.end());
for(int i = ; i < nums.size(); i = i+)
sum += nums[i];
return sum;
}
};
分析2:
看到一种O(n)的解法。使用桶排序,因为note中给定了元素的范围是[-10000,10000],我们可以开辟一个20001大小的数组,将元素的值和数组的索引联系起来,-10000存在n[0]中,10000存在n[20000]中,在按照索引大小对数组进行遍历,通过设定flag的值来交替将数组中的值加到sum中,以起到求两数较小值的功能。不过这种方法牺牲了空间,属于用空间来换时间的做法。
程序2:
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
vector<int> n(, );
for(int i:nums)
n[i+]++;
int flag = ;
int sum = ;
for(int i = ; i < ;){
if((n[i] > )&& flag == ){
sum += (i-);
n[i]--;
flag = ;
}
else if((n[i] > ) && flag == ){
n[i]--;
flag = ;
}
else
i++;
}
return sum;
}
};
LeetCode 561. Array Partition I (C++)的更多相关文章
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 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 ...
- leetcode 561.Array Partition I-easy
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- LeetCode 561 Array Partition I 解题报告
题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...
- [LeetCode] 561. Array Partition I_Easy tag: Sort
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
随机推荐
- Kafka设计解析(十)Kafka如何创建topic
转载自 huxihx,原文链接 Kafka如何创建topic? 目录 一.命令行部分 二.后台逻辑部分 Kafka创建topic命令很简单,一条命令足矣: bin/kafka-topics. --re ...
- 1553: Good subsequence (很奇妙的set模拟题,也可以直接暴力)
1553: Good subsequence Submit Page Summary Time Limit: 2 Sec Memory Limit: 256 Mb Subm ...
- STM32F103 ucLinux开发BOOT
STM32F103 ucLinux开发BOOT STM3210E-EVAL官方开发板主芯片STM32F103ZET6: 片内512K Flash,地址0x0800 0000 ~ 0x0807 FFFF ...
- UML基础—结构和组成
本文主要梳理了一下UML2中的各个图的逻辑划分,UML基础知识. 一.UML2的4个规范 二.UML2的13种模型图 分为3大类:行为视图.交互视图.结构视图 三.UML1和UML2各种视图对照 四. ...
- Mysql 安全登陆工具 mysql_config_editor
mysql_config_editor 帮助信息请查看 man mysql_config_editor 或 mysql_config_editor -? 或 mysql_config_editor s ...
- 一图看懂hadoop MapReduce工作原理
MapReduce执行流程及单词统计WordCount示例
- 2017-2018-1 20155222 《信息安全系统设计基础》第10周 Linux下的IPC机制
2017-2018-1 20155222 <信息安全系统设计基础>第10周 Linux下的IPC机制 IPC机制 在linux下的多个进程间的通信机制叫做IPC(Inter-Process ...
- 2017-2018-2 《网络对抗技术》20155322 Exp7 网络欺诈防范
[-= 博客目录 =-] 1-实践目标 1.1-实践介绍 1.2-实践内容 1.3-实践要求 2-实践过程 2.1-简单应用SET工具建立冒名网站 2.2-ettercap DNS spoof 2.3 ...
- 20155332 补交课后测试——ch11网络编程
20155332 补交课后测试--ch11网络编程 这章的课后测试忘了提交,我课后补做了这章的测试题目,并将知识点和自己的错题汇总如下: 本章知识点总结 11.1 客户端-- 服务器模型 每个网络应用 ...
- 5 数据结构、栈、队列、链表、list、dict、迷宫问题
1.什么是数据结构 2.栈:后进先出 1.什么是栈 栈(Stack)是一个数据集合,可以理解为只能在一端进行插入或删除操作的列表. 2.栈的Python实现 stack = [] stack.ap ...