일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 슬라이딩윈도우
- slidingwindow
- 파이썬기초100제
- 파이썬
- Greedy
- 리트코드
- SQL
- medium
- LeetCode
- 릿코드
- GenerativeAI
- 파이썬알고리즘
- 자연어처리
- 생성형AI
- 니트코드
- 코드업
- nlp
- array
- heap
- Python
- dfs
- sql코테
- stratascratch
- two-pointer
- GenAI
- gcp
- codeup
- 투포인터
- Python3
- 알고리즘
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 |