소프트웨어에 대한 모든 것

LeetCode 풀기 - 1877. Minimize Maximum Pair Sum in Array 본문

알고리즘/LeetCode

LeetCode 풀기 - 1877. Minimize Maximum Pair Sum in Array

앤테바 2021. 11. 6. 23:28
반응형

1877. Minimize Maximum Pair Sum in Array

https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/

 

Minimize Maximum Pair Sum in Array - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

문제)

솔루션1)

오름차순 정렬 후 최소, 최대 pair를 계속 구해나가서 max를 구한다.

 

class Solution:
    def minPairSum(self, nums: List[int]) -> int:
        nums.sort()
        pairs = []
        
        for i in range(int(len(nums)/2)):
            pairs.append(nums[i] + nums[len(nums) - 1 - i])
        return max(pairs)

솔루션2)

class Solution:
    def minPairSum(self, nums: List[int]) -> int:
        nums.sort()
        res = 0
        
        for i in range(len(nums)//2):
            res = max(res, nums[i] + nums[len(nums) - 1 - i])
        return res

 

반응형
Comments