Skip to content

Commit f369d9c

Browse files
committed
Adding Simple Solution - Problem - 561 - Array Partition - I
1 parent b49e5d8 commit f369d9c

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Array_Partition_I/SimpleSolution.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
tmp = [nums[i] for i in range(len(nums)) if i % 2 == 0] #Initialize tmp and update the list by taking odd numbers only
20+
count = 0 #Initialize count
21+
for i in range(len(tmp)): #Loop through tmp
22+
count += tmp[i] #Update the count by sum up the elements
23+
return count #We return the count

0 commit comments

Comments
 (0)