[LeetCode] 561. Array Partition I
2021. 1. 10. 21:28ㆍProblem Solving/LeetCode
https://leetcode.com/problems/array-partition-i/
Array Partition I - 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
class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort(reverse=True) sum = 0 for i in range(1, len(nums), 2): sum += nums[i] return sum
내림차순으로 정렬 -> 두 칸씩 건너뛰며 짝수항 더하기
내 풀이 2
class Solution: def arrayPairSum(self, nums: List[int]) -> int: nums.sort() return sum(nums[0::2])
짝수항만 슬라이싱 한 것의 합
참고
![]() |
|
반응형
'Problem Solving > LeetCode' 카테고리의 다른 글
[LeetCode] 121. Best Time to Buy and Sell Stock (0) | 2021.01.11 |
---|---|
[LeetCode] 238. Product of Array Except Self (0) | 2021.01.10 |
[LeetCode] 15. 3Sum (0) | 2021.01.10 |
[LeetCode] 1. Two Sum (1) | 2021.01.07 |
[LeetCode] 5. Longest Palindromic Substring (0) | 2021.01.07 |