【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 解题报告& ...
随机推荐
- less简介及其编译原理
一.less环境安装 ①首先需要在电脑上安装nodejs,一般会内置npm,利用以下命令可以检测: ②利用npm在线安装less,运行 npm install –g less ③查看是否安装成功,L ...
- HTML5 文件读取
一.定义 input的file类型会渲染为一个按钮和一段文字.点击按钮可打开文件选择窗口,文字表示对文件的描述(大部分情况下为文件名):file类型的input会有files属性,保存着文件的相关信息 ...
- P5589 【小猪佩奇玩游戏】
这题还是比较妙妙套路的,复杂度为\(O(log^2N)\),可以卡掉\(\sqrt n\)的做法 首先我们可以把原数列分成很多个集合,集合之间肯定是两两独立的,考虑分别计算答案 我们定义\(f_i\) ...
- 【JZOJ6225】【20190618】计数
题目 对于一个01串,定义\(f(s)\)为\(f(s) = \sum_{i=0}^{\lfloor \frac{|s|}{2} \rfloor -1 }[s_i=s_{|s|-1-i}]\) 定义\ ...
- 「ZJOI2019」语言
传送门 Description 给定一棵\(n\)个点的树和\(m\)条链,两个点可以联会当且仅当它们同在某一条链上,求可以联会的点的方案数 \(n,m\leq10^5\) Solution 考虑计 ...
- linux core 性能
apt-get install lrzsz apt-get install vim apt-get install -y net-tools apt-get install -y procps htt ...
- [代码质量] Git统计本次提交新增代码行数,建议每个评审commit新增行数小于400行
git log HEAD~1..HEAD --author="$(git config --get user.name)" --pretty=tformat: --numstat ...
- Redis系列 | Redis5.0 新特性
- 清除JAVA 项目中的注释
package com.lookcoder.inschool.utils; import java.io.BufferedReader; import java.io.File; import jav ...
- 必须要注意的 C++ 动态内存资源管理(一)——视资源为对象
必须要注意的 C++ 动态内存资源管理(一)——视资源为对象 一.前言 所谓资源就是,一旦你用了它,将来必须还给系统.如果不这样,糟糕的事情就会发生.C++ 程序中最常见使用的资源就是 ...