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
- uikit toast
- custom ui
- Tuist
- swift bottomsheet
- reactorkit
- custombottomsheet
- BFS
- task cancellation
- SWIFT
- swift 백준
- claen architecture
- paragraph style
- RxSwift
- 버튼 피드백
- custom navigation bar
- swift dashed line
- task cancel
- UIKit
- 타임라인 포맷팅
- swift 점선
- domain data
- swift concurrency
- DP
- rxdatasources
- traits
- identifiable
- button configuration
- swift navigationcontroller
- swift custom ui
- coordinator
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 13305 주유소 본문
문제 요점
- 첫번째 이동할땐 가격에 상관없이 다음 지역까지 갈 만큼의 주유를 진행해야한다
- 도착한 후엔 주유 할 필요가 없으므로 마지막 주유소의 가격은 고려 대상이 아니다.
서브 태스크
첫 풀이
let n = Int(readLine()!)!
let length = readLine()!.split(separator: " ").map{Int($0)!}
let oilPice = readLine()!.split(separator: " ").map{Int($0)!}
var totalLength = length.reduce(0, +)
var result = 0
// 처음엔 가격에 상관없이 넣어야함
result += oilPice[0] * length[0]
totalLength -= length[0]
var minimumOilPrice = oilPice[0]
// 마지막 도착지에선 주유 안해도 됨
for i in 0..<oilPice.count - 1 {
//가장 싼 가격을 구해줌
minimumOilPrice = min(minimumOilPrice, oilPice[i])
}
result += totalLength * minimumOilPrice
print(result)
해당 풀이는 17점 이었습니다.
서브 태스크가 있는 문제는 여럿 봤는데 오답이면 오답이었지 처음으로 부분 점수를 맞아 당황했는데요
개선 된 풀이는 아래와 같습니다
100점 풀이
let n = Int(readLine()!)!
let length = readLine()!.split(separator: " ").map { Int($0)! }
let oilPrice = readLine()!.split(separator: " ").map { Int($0)! }
var result = 0
var minPrice = oilPrice[0]
for i in 0..<n - 1 {
if minPrice > oilPrice[i] {
minPrice = oilPrice[i]
}
result += minPrice * length[i]
}
print(result)
첫번째 코드에서 남은 거리에 최소 가격을 적용했는데 잘못된 접근 방식이었습니다.
조건을 좀 더 신중히 파악해야겠네요
'Algorithm' 카테고리의 다른 글
[Swift 백준] 나무 자르기 (0) | 2025.01.14 |
---|---|
[Swift 백준] 1965 상자넣기 (0) | 2025.01.13 |
[Swift 백준] 15650 N과 M (2) (0) | 2025.01.08 |
[Swift 백준] 15649 N과 M (1) (0) | 2025.01.08 |
[Swift 백준] 11497 통나무 건너뛰기 (0) | 2025.01.08 |