Leetcode - 561. Array Partition I
문제
Loading...
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.
답안
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
sorted_nums = sorted(nums)
return sum([num for idx, num in enumerate(sorted_nums) if not idx % 2])
좀더 간결하게
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
return sum(sorted(nums)[::2])