문제 : https://leetcode.com/problems/minimize-maximum-pair-sum-in-array/
Minimize Maximum Pair Sum in Array - 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
풀이방법
오름차순으로 정렬후 가운데 인덱스를 구해서 양쪽으로 더한것들 중 제일 큰것을 반환한다.
코드
class Solution {
func minPairSum(_ nums: [Int]) -> Int {
let SortedNums = nums.sorted(by: <)
let halfIndex = nums.count/2
var leftIndex = halfIndex - 1
var rightIndex = halfIndex
var max = 0
while leftIndex >= 0 {
if max < (SortedNums[leftIndex] + SortedNums[rightIndex]) {
max = (SortedNums[leftIndex] + SortedNums[rightIndex])
}
leftIndex -= 1
rightIndex += 1
}
return max
}
}
반응형
'알고리즘' 카테고리의 다른 글
[Stack] Validate Stack Sequences - LeetCode Swift (0) | 2021.08.17 |
---|---|
[Stack] 과제는 끝나지 않아! - Baekjoon 17952번 Swift (0) | 2021.08.13 |
[Heap] 우선순위 큐 - LeetCode Swift (0) | 2021.08.11 |
[Stack] daily-temperatures - LeetCode Swift (0) | 2021.08.09 |
[DFS] keys and rooms - LeetCode Swift (0) | 2021.08.09 |