Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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
- SWIFT
- identifiable
- BFS
- Tuist
- 타임라인 포맷팅
- coordinator
- custom ui
- swift 백준
- rxdatasources
- paragraph style
- RxSwift
- domain data
- reactorkit
- 버튼 피드백
- task cancellation
- UIKit
- swift concurrency
- custom navigation bar
- custombottomsheet
- task cancel
- swift custom ui
- swift dashed line
- DP
- uikit toast
- button configuration
- swift bottomsheet
- swift 점선
- swift navigationcontroller
- traits
- claen architecture
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 11497 통나무 건너뛰기 본문
풀이 아이디어만 생각해낸다면 구현자체는 크게 어렵지 않은 문제입니다.
풀이 아이디어
- 입력값들을 정렬한다
- 정렬된 입력값들을 지그재그로 배치해준다(맨앞, 맨뒤로 시작하여 안쪽으로 차곡차곡)
- 가장 큰 차이값을 구한다
자세한 풀이는 코드의 주석 참고
풀이 코드
import Foundation
let testCase = Int(readLine()!)!
for _ in 0..<testCase {
let n = Int(readLine()!)!
var arr = readLine()!.split(separator: " ").map { Int($0)! }
// 입력 값 정렬
arr.sort()
// 난이도 배열 생성, 각 값들은 0으로 초기화
var diffArr = Array(repeating: 0, count: n)
var left = 0
var right = n - 1
// 지그재그로 각 요소들을 분배
for i in 0..<n {
if i % 2 == 0 {
diffArr[left] = arr[i]
left += 1
} else {
diffArr[right] = arr[i]
right -= 1
}
}
// 음수가 나올수도 있으므로 abs를 통해 절대값을 사용
var maxDiff = abs(diffArr[0] - diffArr[n - 1])
for i in 1..<n {
maxDiff = max(maxDiff, abs(diffArr[i] - diffArr[i - 1]))
}
print(maxDiff)
}
'Algorithm' 카테고리의 다른 글
[Swift 백준] 13305 주유소 (0) | 2025.01.09 |
---|---|
[Swift 백준] 15650 N과 M (2) (0) | 2025.01.08 |
[Swift 백준] 15649 N과 M (1) (0) | 2025.01.08 |
[Swift 백준] 15922 아우으 우아으이야!! (0) | 2025.01.08 |
[Swift 백준] 1541 잃어버린 괄호 (0) | 2025.01.08 |