1. Description
Given an integer array nums of 2n integers, group these integers into n pairs \((a_1, b_1), (a_2, b_2), ..., (a_n, b_n)\) such that the sum of \(min(a_i, b_i)\) for all i is maximized. Return the maximized sum.
constraints :
- 1 <= n <= \(10^{4}\)
- nums.length == 2 * n
- \(-10^{4}\) <= nums[i] <= \(10^{4}\)
2. Algorithms
- Start
- Sort the input list.
- Store the length of input list in variable named n.
- Declare variable sum_ initialized with 0.
- Repeat the steps through integers 0 to n - 1 every 2 decimals.
- Increment sum_ by nums[i].
- Return sum_.
- End
3. Codes
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums = sorted(nums)
n = len(nums)
sum_ = 0
for i in range(0, n, 2) :
sum_ += nums[i]
return sum_
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
566 Reshape Matrix (0) | 2022.09.06 |
---|---|
563 Binary Tree Tilt (0) | 2022.09.06 |
559 Maximum Depth of N-ary Tree (0) | 2022.09.06 |
557 Reverse Words in a String III (0) | 2022.09.05 |
551 Student Attendance Record I (0) | 2022.09.05 |