728x90
๋ฌธ์ ์ ๋ชฉ | ์ ๋ต๋ฅ | ๋์ด๋ |
561. array-partition-i | 77.3% | Easy |
Array Partition - LeetCode
Can you solve this real interview question? Array Partition - Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.
leetcode.com
๋ฌธ์ ์์ฝ
2n ๊ฐ์ ์ ์ํ ๋ฐฐ์ด์ด ์ฃผ์ด์ง ๋, ์์์ ๋ ์์๋ก ๊ตฌ์ฑ๋ n๊ฐ์ ์์ด ์๋ค๊ณ ํ์, n๊ฐ์ ์์ ๋ํด ๋ ์์์ค ์ต์๊ฐ์ ํฉ์ ์ต๋๋ฅผ ๊ตฌํ์ฌ๋ผ
์กฐ๊ฑด 1) 2n๊ฐ์ ์ ์ ์ซ์ ๋ฐฐ์ด์ด ์ฃผ์ด์ง๋ค
ํ์ด
Step1. ๋ฐฐ์ด์ ์ ๋ ฌ -> sort() ํจ์
Step2. ํ์๋ฒ์งธ ์์์ ํฉ -> sum(iterable), ์ฌ๋ผ์ด์ฑ ์ด์ฉ
๊ตฌํ (278ms)
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[0::2])
728x90
'์๊ณ ๋ฆฌ์ฆ๐ฅ > ๋ฌธ์ ํ์ด (Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 121. Best_Time_to_Buy_and_Sell_Stock (Easy) 2023/5/3 (0) | 2023.05.03 |
---|---|
[LeetCode] 238. product_of_array_except_self (Medium) 2023/5/2 (0) | 2023.05.02 |
[LeetCode] 15. 3sum (Medium) 2023/5/1 (0) | 2023.05.01 |
[python] 2. ์ฝ๋ฉํ ์คํธ์์ ์ฌ์ฉ๋๋ Python ๊ธฐ๋ณธ (0) | 2023.04.30 |
[LeetCode] 1. two_sum (Easy) 2023/4/29 (0) | 2023.04.29 |