【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));
    }

}

原文地址:https://www.cnblogs.com/cutter-point/p/11128142.html

时间: 2024-08-03 05:43:28

【LEETCODE】39、第561题 Array Partition I的相关文章

Leetcode刷题记录[python]——561 Array Partition I

一.前言 二.题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. Example 1: Inp

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

题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. 示例 1: 输入: [1,4,3,2] 输出: 4 解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4). 提示: n 是正整数,范围在 [1, 10000]. 数组中的元素范围在 [-10000, 10000]. 思路 分组之后min(ai, bi)的和最大

[array] leetcode - 39. Combination Sum - Medium

leetcode - 39. Combination Sum - Medium descrition Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from

LeetCode: Search in Rotated Sorted Array

LeetCode: Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

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

Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the arr

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

LeetCode面试常见100题( TOP 100 Liked Questions) 置顶 2018年07月16日 11:25:22 lanyu_01 阅读数 9704更多 分类专栏: 面试编程题真题合集 常见算法问题 LeetCode试题 LeetCode常见试题 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/lanyu_01/article/details/81062232 这篇文章

[LeetCode] Search in Rotated Sorted Array [35]

题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no dup

[LeetCode] Remove Duplicates from Sorted Array II [27]

题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2,3]. 原题链接(点我) 解题思路 移除数组中重复次数超过2次以上出现的数,但是可以允许重复2次

[leetcode]Search in Rotated Sorted Array @ Python

原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array/ 题意: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in