[easy] leetcode 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:
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).
Note:
n is a positive integer, which is in the range of [1, 10000].
All the integers in the array will be in the range of [-10000, 10000].

代碼是:

class Solution(object):
    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.sort()
        sum,i = 0,0
        while i < len(nums):
            sum += nums[i]
            i += 2
        
        return sum
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 背景 一年多以前我在知乎上答了有關(guān)LeetCode的問題, 分享了一些自己做題目的經(jīng)驗。 張土汪:刷leetcod...
    土汪閱讀 12,901評論 0 33
  • 文/史逍遙 10.三英戰(zhàn)呂布 反董第一槍打響,長沙太守孫堅打頭陣,率隊攻打洛陽汜水關(guān)。 但是,在任何年代和任何時候...
    史逍遙閱讀 3,912評論 0 0
  • 晨, 寧靜而安詳, 洗去昨夜繁華和嘈雜。 晨, 希望在發(fā)芽, 一天之計在于晨, 奮斗開始吧! 晨,奔跑, 晨,加油!
    無限極夏蘭770621閱讀 229評論 0 0
  • 3.28 我最近看的書多了,給我?guī)淼氖且环N知識信息的爆炸。感覺到了頭腦里的想法不斷的冒出來,一個接著一個,給我造...
    昨日的記憶閱讀 354評論 0 0
  • 關(guān)于“徐歌陽不雅視頻事件”晨楠少彬微博誤傷葛薈婕女士一事,晨楠少彬回應(yīng)微博賬號被盜,晨楠少彬發(fā)微博向葛薈婕女士,微...
    簡書娛樂頻道閱讀 404評論 0 2

友情鏈接更多精彩內(nèi)容