[leetcode: Python]561. Array Partition I
来源:互联网 发布:软件书生商务网 编辑:程序博客网 时间:2024/06/06 01:35
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: 4Explanation: n is 2, and the maximum sum of pairs is 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].
方法一:58ms
解题思路:
排序(Sort)
将数组从小到大排序,取下标为偶数的元素求和即为答案。
class Solution(object): def arrayPairSum(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() return sum(nums[::2])
阅读全文
0 0
- [leetcode: Python]561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode 561. Array Partition I
- [LeetCode]561. Array Partition I
- leetcode 561. Array Partition I
- LeetCode 561. Array Partition I
- Leetcode 561. Array Partition I
- [leetcode]561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode: 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode: 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode ** 561. Array Partition I
- leetcode.561.Array Partition I
- 【leetcode】561. Array Partition I
- [LeetCode]561.Array Partition I
- [leetcode]561. Array Partition I
- openssl之aes加密(AES_cbc_encrypt 与 AES_encrypt 的编程案例)
- cocos2d导演类中的方法即使用
- linux的centos 6.8 搭建cacti监控平台
- 判断url是否存在某个字符
- Combination Sum II
- [leetcode: Python]561. Array Partition I
- Jquery第二篇【选择器、DOM相关API、事件API】
- leetcode
- spring boot 官方文档翻译之整合 mybatis
- Javascript 异步特性实验
- C 格式化输出符
- 归并排序
- android sdk 更新
- layDate显示默认时间