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 점선
- swift bottomsheet
- UIKit
- custom ui
- custombottomsheet
- traits
- swift custom ui
- DP
- button configuration
- domain data
- swift 백준
- SWIFT
- task cancellation
- BFS
- paragraph style
- uikit toast
- Tuist
- rxdatasources
- swift concurrency
- RxSwift
- custom navigation bar
- 타임라인 포맷팅
- claen architecture
- coordinator
- reactorkit
- 버튼 피드백
- swift navigationcontroller
- swift dashed line
- task cancel
- identifiable
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 11052 카드 구매하기 본문
https://www.acmicpc.net/problem/11052
풀이 아이디어
- 문제를 요약하면 결국 n개의 카드를 가장 비싸게 사는 법을 구하기
- 즉, 카드 개수를 정확히 N개가 되도록 여러 개의 카드팩을 조합해서 가장 비싸게 구매하는 방법을 찾기
- DP 사용
점화식은 아래와 같다
dp[i] = max(dp[i], dp[i-j] + input[j-1])
- i장을 구매할 때,
- j장짜리 카드팩을 한 번 사고,
- 나머지 i-j장을 가장 비싸게 살 방법을 dp[i-j]에서 가져오는 것.
풀이
let n = Int(readLine()!)!
let input = readLine()!.split(separator: " ").map {Int($0)!}
var dp = [Int](repeating: 0, count: n+1)
for i in 1..<n+1 {
for j in 1..<i+1 {
dp[i] = max(dp[i], dp[i-j]+input[j-1])
}
}
print(dp[n])
'Algorithm' 카테고리의 다른 글
[Swift 백준] 10610 30 (0) | 2025.04.14 |
---|---|
[Swift 백준] 1269 대칭 차집합 (0) | 2025.04.10 |
[Swift 백준] 1475 방 번호 (0) | 2025.03.31 |
[Swift 백준] 9465 스티커 (0) | 2025.03.28 |
[Swift 백준] 7569 토마토 (0) | 2025.03.26 |