Skip to content

Commit b4d7bad

Browse files
committed
Adding Efficient Solution - Problem - 561 - Array Partition - I
1 parent f369d9c commit b4d7bad

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
##==================================
2+
## Leetcode
3+
## Student: Vandit Jyotindra Gajjar
4+
## Year: 2020
5+
## Problem: 561
6+
## Problem Name: Array Partition I
7+
##===================================
8+
#
9+
#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.
10+
#
11+
#Example 1:
12+
#Input: [1,4,3,2]
13+
#
14+
#Output: 4
15+
#Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
16+
class Solution:
17+
def arrayPairSum(self, nums):
18+
nums.sort() #Sort the nums list
19+
count = 0 #Initialize count
20+
for i in range(0, len(tmp), 2): #Loop through tmp, starts from 0th index to len(tmp) taking 2 steps
21+
count += nums[i] #Update count by taking sum of the elements
22+
return count #We return count

0 commit comments

Comments
 (0)