leetcode 561

来源:互联网 发布:贪心算法会场安排问题 编辑:程序博客网 时间:2024/06/10 14:52

题目: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.

方法:将数组从小到大排序,然后选择索引为偶数的数相加。

翻译:给2n个数的数组,把它们分为n组,取每组中最小的数使其相加和最大。

思想:贪心算法。理解:简单点说一组已经从小到排好序的数组,从最大值开始,最大值肯定是取不到的,那么,取次大的数,也就是倒数第二个数,以此类推,顺其自然,都是取的索引位置为偶数的数。

Java函数:Arrays.sort();可以自动对数组排序。

这里写图片描述

0 0
原创粉丝点击