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的更多相关文章

  1. LeetCode算法题-Array Partition I(Java实现)

    这是悦乐书的第262次更新,第275篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第129题(顺位题号是561).给定一个2n个整数的数组,你的任务是将这些整数分组为n对 ...

  2. Leetcode#561. Array Partition I(数组拆分 I)

    题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...

  3. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  4. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  5. [array] leetcode - 39. Combination Sum - Medium

    leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without dup ...

  6. LeetCode面试常见100题( TOP 100 Liked Questions)

    LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题 ...

  7. 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 ...

  8. LeetCode:Remove Duplicates from Sorted Array I II

    LeetCode:Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place su ...

  9. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

随机推荐

  1. nexus 3.17.0 做为golang 的包管理工具

    nexus 3.17.0 新版本对于go 包管理的支持是基于go mod 的,同时我们也需要一个athens server 然后在nexus 中配置proxy 类型的repo 参考配置 来自官方的配置 ...

  2. circus 做为批处理的守护进程

    circus 是集成了zeromq,使用python编写的一个进程以及socket 管理工具,使用circus 的进程管理,我们可以用来进行批任务的 处理,同时又能保证任务的准确 项目使用docker ...

  3. hotspot编译

    "AA=="1",==", /usr/bin/make -s VERBOSE="-s" LOG_LEVEL="warn" ...

  4. 刷题记录:[De1CTF 2019]SSRF Me

    目录 刷题记录:[De1CTF 2019]SSRF Me 一.涉及知识点 1.MD5长度扩展攻击 2.Python 2.x - 2.7.16 urllib.fopen支持local_file导致LFI ...

  5. Dolly

    dolly - 必应词典 美['dɑli]英['dɒli] n.洋娃娃:(搬运重物的)台车 v.用独轮车运(物):用搅拌棒洗(衣):用捣棒捣碎(矿石) 网络多莉:多利:移动式摄影小车 变形复数:dol ...

  6. np.stack

  7. js object 添加键值

    第一种方法let obj ={"name":"tom","age":16}let key = "id";let valu ...

  8. php读写xml基于DOMDocument方法

    1.读xml内容: xml文件plays.xml文档结构: <?xml version="1.0" encoding="UTF-8"?> <P ...

  9. (三)用CONCAT 函数 拼接字段

    一.将两个列拼接成一个列 数据源 select CONCAT(TRIM(username),'(',locaiton,')') from user2 解释: TRIM()函数用于去除字符串左右两边的空 ...

  10. [LeetCode] 533. Lonely Pixel II 孤独的像素 II

    Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...