leetcode561: Array Partition I
来源:互联网 发布:linux下启动tomcat服务 编辑:程序博客网 时间:2024/06/07 07:04
要求:
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: 4Explanation: 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].
比较好奇出题的人是怎么想的。。。
public int arrayPairSum(int[] nums) {ArrayList list = new ArrayList();int result = 0;for (int i = 0; i < nums.length; i++)list.add(nums[i]);list.sort(null);for (int i = 0; i < list.size(); i += 2)result += (int) list.get(i);return result;}
阅读全文
0 0
- leetcode561. Array Partition I
- leetcode561. Array Partition I
- leetcode561: Array Partition I
- LeetCode561. Array Partition I
- Leetcode561. Array Partition I
- python--leetcode561. Array Partition I
- 7.leetCode561.:Array Partition I
- Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- 561. Array Partition I
- Array Partition I
- 2017年7月,桌面分享
- java随机生成6位随机数 5位随机数 4位随机数
- Django-中间件Middleware
- QStackedWidget实现左侧列表与右侧控件关联
- 基于 Vue、Electron、Node、Koa、Python 等构建的独立音乐社区客户端!
- leetcode561: Array Partition I
- java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/ObjectMapper
- 在 SQLite 中使用 CSV
- C++连接数据库并实现读写操作
- i=i++问题
- java web项目 路径url有中文,图片是中文,tomcat404解决方案
- 贪心的一道题 Supermarket
- Struts2结果视图配置---【小白系列】0基础到熟练应用struts2框架(二)
- 设计模式之代理模式