일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 파이썬알고리즘
- 생성형AI
- Stack
- SQL
- Python3
- slidingwindow
- tree
- codeup
- 니트코드
- 파이썬
- sql코테
- medium
- GenAI
- nlp
- GenerativeAI
- 투포인터
- gcp
- 코드업
- Python
- dfs
- heap
- two-pointer
- 리트코드
- 파이썬기초100제
- LeetCode
- 슬라이딩윈도우
- 자연어처리
- stratascratch
- 릿코드
- 알고리즘
Archives
- Today
- Total
Tech for good
[Leetcode/Sort, Greedy] 561. Array Partition 본문
class Solution:
def arrayPairSum(self, nums: List[int]) -> int:
nums.sort()
return sum(nums[::2])
🧠 Greedy Strategy:
To maximize the sum of the minimums of all n pairs, you should:
- Sort the array
- Always pair adjacent elements: (nums[0], nums[1]), (nums[2], nums[3]), ...
This ensures that the smaller number in each pair is as large as possible — which directly contributes to the final sum.
'IT > Computer Science' 카테고리의 다른 글
[Leetcode/Array, Greedy] 860. Lemonade Change (0) | 2025.05.05 |
---|---|
[Leetcode/Sort, Greedy, Two-pointer] 455. Assign Cookies (0) | 2025.05.03 |
[Leetcode/Stack,Queue] 1700. Number of Students Unable to Eat Lunch (0) | 2025.04.22 |
[Leetcode/Stack] 1614. Maximum Nesting Depth of the Parentheses (0) | 2025.04.21 |
[Leetcode/Stack,Tree] 897. Increasing Order Search Tree (0) | 2025.04.16 |