【LeetCode】数组-6(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.

感觉题目的大致意思就是把数组分成很多个二元数组,对它们以求最小值的方式求和,使得和最大。
思路:
最开始的思路,现在还不知道对不对,就是先排序数组,使用一个指针向后遍历,求最小并求和。⚠️【注意】指针每次累加 2
【正确代码】 一次写对~
class Solution {
public int arrayPairSum(int[] nums) {
if (nums.length % 2 != 0 || nums == null) {
return -1;
}
Arrays.sort(nums);
int maxSum = 0;
for (int i = 0; i < nums.length - 1; i += 2) {
maxSum += Math.min(nums[i], nums[i + 1]);
}
return maxSum;
}
}
时间复杂度:O(n*logn)
空间复杂度:O(n*logn)
【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)的更多相关文章
- [LeetCode&Python] Problem 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(数组拆分 I)
题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...
- 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 (数组分隔之一)
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- LeetCode 561:数组拆分 I Array Partition I
文章全部来自公众号:爱写bug 算法是一个程序的灵魂. Given an array of 2n integers, your task is to group these integers into ...
- 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 (C++)
题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...
随机推荐
- java中的方法引用(method reference)官方文档总结
2017/7/5 转载写明出处:http://www.cnblogs.com/daren-lin/p/java-method-reference.html 今天要说的是java中的一项新特性,方法引用 ...
- MySQL实例搭建
Q:如何判断一个Linux系统具备安装MySQL的条件? A: 1.Linux网络已经配置完成 ip地址/子网掩码.默认网关.主机名字 /etc/hosts:访问这个数据库的应用的IP地址和主机名字也 ...
- VB6之切换桌面
Desktop的API,用于切换或者系统桌面环境.扩展起来可以做一个锁屏程序或者多桌面程序. 模块部分: 'desktop.bas 'too much struct and declare unuse ...
- English - Green Peanut Butter
There is a guy. He wants to drink 12 cups of green peanut butter. He needs green peanut butter. So h ...
- Struts2国际化信息机制
国际化信息机制 (三种 Action范围. Package范围. 全局) 1. 全局国际化配置信息文件 全局国际化文件,对所有Action 生效,任何程序都可以访问到,需要在struts.xml 配 ...
- 利用VNC远程登录Linux服务器简易版
我负责管理实验室的一台服务器,安装的系统是CentOS 6.7.使用pietty远程登录服务器(命令行) 需求:使实验室的同学和老师使用RealVNC远程登录服务器. 一,首先检查一下服务器是否安装V ...
- Oracle数据迁移-系统数据合并笔记
创建临时表:execute immediate 'sql'; 通过临时表和关联查询解决循环处理效率低下,大数据操作移植时时间太长的问题. 结构相同的系统数据库表移植,案例如下: create or r ...
- asp.net验证码的编写
很多时候我们在登录什么网站的时候,除了需要什么用户名和密码之外,有的还需要验证码那么在asp.net中这个验证码如何编写和设计,今天我就来给大家说一下: 首先创建一个页面名字随便起一个,我们这里叫做C ...
- JavaScript 值类型和引用类型的初次研究
今天遇到一个坑,具体的不多说,直接上代码 var a = [ [],[],[1,2,3] ] var b = ['颜色','大小','尺寸'] var arr = [] for(let i = 0; ...
- 51nod_1265:四点共面(计算几何)
题目链接 设四点为a_0~3,若共面则 (a1a0*a2a0)·a3a0=0 #include<iostream> #include<cstdio> #include<c ...