【LEETCODE】39、第561题 Array Partition I
package y2019.Algorithm.array; /**
* @ProjectName: cutter-point
* @Package: y2019.Algorithm.array
* @ClassName: ArrayPairSum
* @Author: xiaof
* @Description: 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.
*
* 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).
*
* 给定一个长度为2n(偶数)的数组,分成n个小组,返回每组中较小值的和sum,使sum尽量大
* @Date: 2019/7/2 17:24
* @Version: 1.0
*/
public class ArrayPairSum { public int solution(int[] nums) {
//这个题的求每组中小的值,最后求和,尽量大,那就是说相近的数据最好放一组,不然差距很大,会导致最后值相差很大
quikSort(nums, 0, nums.length);
//排完序之后,叉开获取数据和即可
int result = 0;
for(int i = 0; i < nums.length; i += 2) {
result += nums[i];
}
return result;
} private void quikSort(int[] array, int left, int right) {
if(left < right) {
int mid = partitionSort(array, left, right);
quikSort(array, left, mid);
quikSort(array, mid + 1, right);
}
} private int partitionSort(int[] array, int left, int right) {
// if(left == right || left > right) {
// return left;
// } int midValue = array[left];
int start = left;
int end = right; //分区排序
do { do { ++ start; } while(start < right && array[start] < midValue); do {
--end;
} while(left < end && array[end] > midValue); //交换
if(start < end) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
} } while(start < end); //交换完毕之后,最后吧坑填上,这个时候left和right错开一位,所以right再left的左边
array[left] = array[end];
array[end] = midValue; return end;
} public static void main(String args[]) {
int A1[] = {1,4,3,2};
ArrayPairSum fuc = new ArrayPairSum();
System.out.println(fuc.solution(A1));
} }
【LEETCODE】39、第561题 Array Partition I的更多相关文章
- LeetCode算法题-Array Partition I(Java实现)
这是悦乐书的第262次更新,第275篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第129题(顺位题号是561).给定一个2n个整数的数组,你的任务是将这些整数分组为n对 ...
- 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 ...
- [array] leetcode - 39. Combination Sum - Medium
leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...
- LeetCode面试常见100题( TOP 100 Liked Questions)
LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...
- LeetCode:Search in Rotated Sorted Array I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode:Remove Duplicates from Sorted Array I II
LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
随机推荐
- PHP 打印输出数组内容及结构 print_r 与 var_dump 函数
利用 print_r() 函数可以打印输出整个数组内容及结构,按照一定格式显示键和元素.注意 print_r() 函数不仅是只用于打印,实际它是用于打印关于变量的易于理解的信息. 例子1 <?p ...
- 懵了!简单的HTTP调用,时延竟如此大?
最近项目测试遇到个奇怪的现象,在测试环境通过 Apache HTTP Client 调用后端的 HTTP 服务,平均耗时居然接近 39.2ms. 图片来自 Pexels 可能乍一看觉得这不是很正常吗, ...
- MVC设计模式和三层架构
JavaEE设计模式 1.传统设计模式(现在几乎不再使用): Jsp + javaBean, JavaBean用来对应数据库中的表,jsp负责显示界面.接受请求.处理业务.访问数据库. 弊端: 业务多 ...
- Zookeeper运维常用四字命令
Zookeeper运维常用四字命令 echo stat|nc 127.0.0.1 2181 查看哪个节点被选择作为follower或者leader 使用echo ruok|nc 127.0.0.1 2 ...
- iostat参数说明
一直不太会用这个参数.现在认真研究了一下iostat,因为刚好有台重要的服务器压力高,所以放上来分析一下.下面这台就是IO有压力过大的服务器 # iostat -x 1 10 Linux 2.6.18 ...
- Java多个线程顺序打印数字
要求 启动N个线程, 这N个线程要不间断按顺序打印数字1-N. 将问题简化为3个线程无限循环打印1到3 方法一: 使用synchronized 三个线程无序竞争同步锁, 如果遇上的是自己的数字, 就打 ...
- Linux下emacs如何安装
ftp://ftp.gnu.org/gnu/emacs/ 下载对应版本编译安装 cd 自己想安装的目录 sudo wget ftp://ftp.gnu.org/gnu/emacs/emacs-24.4 ...
- IDEA中不编译src/main/java目录下的*.xml文件
使用idea构建maven项目时不编译src/main/java目录下写的mapper.xml文件,这是找到pom.xml文件,在<build>节点下添加如下代码: <!-- map ...
- rqalpha学习-1
1 setup 安装 C:\work\python\rqalpha\setup.py install C:\work\python\rqalpha 2 mod list 列出mod C:\work\p ...
- [LeetCode] 529. Minesweeper 扫雷
Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...