【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 ...
随机推荐
- php之试触法----error--关键字的误用
实际开发中,在不同网页的输出中,常常有许多公共的代码或者变量需要使用,于是定义了以下类来缩减代码量 如下代码所示: <?php class universalClass { function w ...
- 由max_allowed_packet引发的mysql攻防大战
1.原因 程序的sql语句比较长.max_allowed_packet默认是1024.于是就报错了.一开始手动改 global max_allowed_packet ,改完后.莫名奇妙被还原.后来改配 ...
- 前端页面——Cookie与Session有什么区别
我们在实际生活中总会遇到这样的事情,我们一旦登录(首次输入用户名和密码)某个网站之后,当我们再次访问的时候(只要不关闭浏览器),无需再次登录.而当我们在这个网站浏览一段时间后,它会产生我们浏览的记录, ...
- 51nod_1627:瞬间移动
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1627 还是杨辉三角~ #include<bits/st ...
- (一)SQL关联查询的使用技巧 (各种 join)
---恢复内容开始--- (一)SQL关联查询的使用技巧 (各种 join) 这几天因为工作的时候,发现自己的sql语句基础不是很好,特意研究了一下,发现sql语句真的是博大精深,sql语句不仅是要查 ...
- maven - 引用本地jar,进行jar包移动
背景: 项目为maven工程,部分jar需要需用项目单独修改的本地jar包. 配置好scope后发现构建后引用的jar没有移动到对应的目录,百度后发现需要配置以下依赖 <plugin> & ...
- SpringMVC源码情操陶冶-FreeMarker之web配置
前言:本文不讲解FreeMarkerView视图的相关配置,其配置基本由FreeMarkerViewResolver实现,具体可参考>>>SpringMVC源码情操陶冶-ViewRe ...
- Go语言学习笔记(五)文件操作
加 Golang学习 QQ群共同学习进步成家立业工作 ^-^ 群号:96933959 文件读取 os.File 封装了文件相关操作 type File File代表一个打开的文件对象. func Cr ...
- Spring源码情操陶冶-AbstractApplicationContext#registerListeners
承接前文Spring源码情操陶冶-AbstractApplicationContext#onRefresh 约定web.xml配置的contextClass为默认值XmlWebApplicationC ...
- 【MYSQL】主从库查看及搭建
show slave status 查看从库信息 http://blog.csdn.net/lxpbs8851/article/details/7898716 搭建主从库 http://www. ...