leetcode试水--561. Array Partition I

来源:互联网 发布:知乎福利收藏夹 编辑:程序博客网 时间:2024/06/05 07:54

看了几天python来练手,第一次用leetcode不太会,于是找了最水的题,结果还真不会用再见

可能刷几个题之后会好一点吧?

题目:https://leetcode.com/problems/array-partition-i/description/

排序,其实反证法可能更容易理解一点,后面只要证明任意非升序序列不满足要求就好

class Solution(object):    def arrayPairSum(self, nums):        """        :type nums: List[int]        :rtype: int        """        nums.sort()        i = 0        res = 0        while i < len(nums):            res += nums[i]            i += 2        return res