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 custom ui
- uikit toast
- SWIFT
- swift navigationcontroller
- RxSwift
- swift dashed line
- claen architecture
- button configuration
- custombottomsheet
- swift concurrency
- swift 백준
- traits
- BFS
- custom navigation bar
- rxdatasources
- custom ui
- 버튼 피드백
- coordinator
- reactorkit
- UIKit
- Tuist
- 타임라인 포맷팅
- swift 점선
- domain data
- identifiable
- swift bottomsheet
- task cancellation
- task cancel
- paragraph style
- DP
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 2512 예산 본문
풀이 아이디어
이분탐색 문제입니다.
이분탐색에선 최소 최댓값의 기준을 잡는 것이 중요합니다.
해당 문제에서 1번 조건으로 모든 요청이 배정될 수 있는 경우에는 요청한 금액을 그대로 배정한다 했으므로
최댓값은 input의 max값이 됩니다.
나머지는 일반적인 이분탐색이고, 핵심부분에 주석으로 설명을 덧붙였습니다
풀이
import Foundation
let n = Int(readLine()!)!
var input = readLine()!.split(separator: " ").map { Int($0)! }
let total = Int(readLine()!)!
var left = 0
var right = input.max()!
var result = 0
while left <= right {
let mid = (left + right) / 2
// 현재 상한액(mid)로 배정한 예산의 총합 계산합니다
// min($0, mid)를 통해, 만약 mid가 내 상환액보다 크다면 내 상환액을 택합니다.
let allocated = input.map { min($0, mid) }.reduce(0, +)
if allocated <= total {
result = mid
left = mid + 1
} else {
right = mid - 1
}
}
print(result)
'Algorithm' 카테고리의 다른 글
[Swift 백준] 18870 좌표 압축 (0) | 2025.01.20 |
---|---|
[Swift 백준] 17266 어두운 굴다리 (0) | 2025.01.16 |
[Swift 백준] 나무 자르기 (0) | 2025.01.14 |
[Swift 백준] 1965 상자넣기 (0) | 2025.01.13 |
[Swift 백준] 13305 주유소 (0) | 2025.01.09 |