Skip to content

Commit eda5e9c

Browse files
authored
Create ArrayPartitionI.py
1 parent 6877990 commit eda5e9c

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

ArrayPartitionI.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
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.
3+
4+
Example 1:
5+
Input: [1,4,3,2]
6+
7+
Output: 4
8+
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
9+
Note:
10+
n is a positive integer, which is in the range of [1, 10000].
11+
All the integers in the array will be in the range of [-10000, 10000].
12+
13+
'''
14+
15+
class Solution(object):
16+
def arrayPairSum(self, nums):
17+
"""
18+
:type nums: List[int]
19+
:rtype: int
20+
"""
21+
nums.sort()
22+
res = 0
23+
for i in xrange(0, len(nums), 2):
24+
res += nums[i]
25+
26+
return res
27+

0 commit comments

Comments
 (0)