美文网首页
561. Array Partition I

561. Array Partition I

作者: 鱼欲遇雨 | 来源:发表于2020-01-25 21:21 被阅读0次

解法一

  1. 排序
  2. 每隔一个数,进行相加

pythonic写法: sum(sorted(nums)[::2])

思想
每一对中最大值,使用尽量小的。这样下一组的最小值才会尽量的大。从而使每一对的最小值相加才会变成最大的。贪心算法。

解法二

数学证明

注意点

  • python的sort使用方法
    • 基于原有的list进行排序: list.sort()
    • 返回新的排序后的list: sorted(list) 针对所有的可迭代对象

相关文章

网友评论

      本文标题:561. Array Partition I

      本文链接:https://www.haomeiwen.com/subject/uhorthtx.html