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. sqler 集成 terraform v0.12 生成资源部署文件

    terraform v0.12 发布了,有好多新功能的添加,包括语法的增强,新函数的引入,更好的开发提示 只是当前对于一些老版本的provider 暂时还不兼容,但是大部分官方的provider 都是 ...

  2. [RN] React Native 使用 react-native-vector-icons 图标显示问号

    我在第一次使用 react-native-vector-icons 时图标显示问号 后来在网上查了很多文章,发现原因有两个 1)安装完 react-native-vector-icons 后,没有li ...

  3. 洛谷 P1063 能量项链 题解

    P1063 能量项链 题目描述 在\(Mars\)星球上,每个\(Mars\)人都随身佩带着一串能量项链.在项链上有\(N\)颗能量珠.能量珠是一颗有头标记与尾标记的珠子,这些标记对应着某个正整数.并 ...

  4. csp退役前的做题计划1(真)

    csp退役前的做题计划1(真) 因为我太菜了,所以在第一次月考就会退役,还是记录一下每天做了什么题目吧. 任务计划 [ ] Z算法(Z Algorithm) 9.28 [x] ARC061C たくさん ...

  5. 为什么GPU不能代替CPU?

    gpu就是并行处理强大, cpu很多功能gpu都没有. 什么指令流水化, 多进程管理之类的. gpu没有多少自主处理指令的能力, 基本是指令靠cpu 计算靠gpu.GPU工作原理是cpu 处理指令,遇 ...

  6. Intellij idea 告警:'while' statement cannot complete without throwing an exception

    有时候这个告警是多余的,例如我们手写的监控线程. 如果有消除告警强迫症.在线程的执行方法上加入注解. @SuppressWarnings("InfiniteLoopStatement&quo ...

  7. [转]vscode 插件推荐 - 献给所有前端工程师(2019.8.7更新)

    原文地址:https://segmentfault.com/a/1190000006697219 VScode现在已经越来越完善.性能远超Atom和webstorm,你有什么理由不用它?在这里,我会给 ...

  8. 深入理解vue中的slot与slot-scope

    from:https://segmentfault.com/a/1190000012996217?utm_source=tag-newest 写在前面 vue中关于插槽的文档说明很短,语言又写的很凝练 ...

  9. Hashmap(类似字典的东西)

    注意: 键值是唯一的,1个键对应一个值 常用api 打印处字典直接println方法 判断是否存在key值     containsKey() 例子: 基础操作 https://ke.qq.com/w ...

  10. 使用consul实现分布式服务注册和发现--redis篇

    安装consul client consul 客户端检脚本 ====================================================================== ...