561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say \((a_1, b_1), (a_2, b_2), ..., (a_n, b_n)\) which makes sum of \(min(a_i, b_i)\) 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].
自家代码:
先排序, 索引为偶数元素求和.
副产品为: vector的排序方式 sort(A.begin(), A.end()).
\(O(nlogn)\) time, \(O(1)\) extra space.
int arrayPairSum(vector<int>& A) {
sort(A.begin(), A.end());
int sum = 0;
for (int i = 0; i < A.size(); i += 2) {
sum += A[i];
}
return sum;
}
561. Array Partition I的更多相关文章
- Leetcode#561. Array Partition I(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 561. Array Partition I【easy】
561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...
- 561. Array Partition I - LeetCode
Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...
- 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)
题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...
- 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 ...
- 【easy】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 解题报告
题目要求 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 ...
随机推荐
- 重启网卡报错:Device eth0 does not seem to be present
ifconfig...没有看到eth0..然后重启网卡又报下面错误. 故障现象: service network restartShutting down loopback insterface: ...
- 用UIWebView加载本地图片和gif图
加载gif图: NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@" ...
- Spring之IOC(一)
Spring之IOC(一) 对于Spring的基本概念在这里就不在赘述了. Spring的最核心的两部分则是AOP(面向切面编程)和IOC(控制反转).本篇文章主要讲我对IOC的理解. IOC:即In ...
- flask 操作mysql的两种方式-sqlalchemy操作
flask 操作mysql的两种方式-sqlalchemy操作 二.ORM sqlalchemy操作 #coding=utf-8 # model.py from app import db class ...
- hasattr(obj,attr) 判断前面是否有后面的属性
hasattr(obj,attr) 判断前面是否有后面的属性
- Java中的变量数据类型补充
Java中变量的数据类型的补充 变量按照数据类型进行分类 1.基本数据类型 数值型:①整数类型(byte,short,int,long)②浮点型(float,doubbe)③字符型(char)④布尔型 ...
- jQuery系列 第一章 jQuery框架简单介绍
第一章 jQuery框架简单介绍 1.1 jQuery简介 jQuery是一款优秀的javaScript库(框架),该框架凭借简洁的语法和跨平台的兼容性,极大的简化了开发人员对HTML文档,DOM,事 ...
- Windows下Java开发环境安装与配置
1. 前往Oracle网站下载JDK程序并安装. http://www.oracle.com/technetwork/java/javase/downloads/index.html 目前最新的版本为 ...
- [NOI 2005]聪聪和可可
Description 题库链接 一只猫和一只老鼠在一张 \(n\) 个节点和 \(m\) 条边的无向图上,初始位置不同.对于每一时刻,猫会先走,它走的方向为靠近老鼠的方向:若多个节点可选,则选字典序 ...
- [BJOI2006]狼抓兔子
题目描述 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨,它们只有两个窝,现在你做为狼王,面对下面这样一个网格的地形: ...