Minimize Maximum Pair Sum in Array — LeetCode 1877 Python Solution
MediumGreedyArrayTwo PointersSorting
- Problem
- #1877
- Pattern
- Two Pointers
- Reading time
- 2 min
- Source
- leetcode.com
The problem
The pair sum of a pair (a,b) is equal to a + b. The maximum pair sum is the largest pair sum in a list of pairs.
Example
- Input
- nums = [3,5,2,3]
- Output
- 7
- Explanation
- The elements can be paired up into pairs (3,3) and (5,2).
Python solution
Python
class Solution:
def minPairSum(self, nums: List[int]) -> int:
nums.sort()
return max(x + nums[-i - 1] for i, x in enumerate(nums[: len(nums) >> 1]))Complexity
| Measure | Complexity |
|---|---|
| Time | O(n \times \log n) |
| Space | O(\log n) auxiliary |
Pattern: Two Pointers
Use the order already in the input to discard half the search space at every step. LeetCode 1877. Minimize Maximum Pair Sum in Array is filed here because LeetCode tags it Two Pointers, which is the vocabulary this hub collects.
The two pointers guide has the Python template for the pattern and the 201 LeetCode problems that use it.
Related problems
Frequently asked questions
- How hard is LeetCode 1877. Minimize Maximum Pair Sum in Array?
- LeetCode 1877. Minimize Maximum Pair Sum in Array is rated Medium on LeetCode.
- What is the time complexity of LeetCode 1877. Minimize Maximum Pair Sum in Array?
- The Python solution on this page runs in O(n \times \log n).
- What is the space complexity of LeetCode 1877. Minimize Maximum Pair Sum in Array?
- The Python solution on this page uses O(\log n) auxiliary space.
- What topics does LeetCode 1877. Minimize Maximum Pair Sum in Array cover?
- LeetCode 1877. Minimize Maximum Pair Sum in Array is tagged Greedy, Array, Two Pointers and Sorting on LeetCode.