diff --git a/sep037/DP/BOJ_9095.swift b/sep037/DP/BOJ_9095.swift new file mode 100644 index 0000000..6ce923a --- /dev/null +++ b/sep037/DP/BOJ_9095.swift @@ -0,0 +1,40 @@ +// +// main.swift +// 9095 +// +// Created by Seungeun Park on 5/11/25. +// + +import Foundation + +let N = Int(readLine()!)! + +var number : [Int] = [] + +for _ in 0 ..< N { + number.append(Int(readLine()!)!) +} + +func dp(N : Int) -> Int { + //var array = [Int](repeating: 0, count: N + 1) + // 처음에 생각 안 하고 N개 만들었다가 N에 2나 1 같은 게 들어올 수 있는 걸 대응 안 했다. + // 만약 1이 들어왔을 경우 아래 초기화는 bound를 넘어서게 된다. 그렇다면 최소 배열을 4로 만들어주면 됨 ! + var array = [Int](repeating: 0, count: max(N + 1, 4)) + + array[1] = 1 + array[2] = 2 + array[3] = 4 // 3까지는 자기 자신을 가지고 있기 때문에 초기화 해줘야 함 + + if N >= 4 { // 여기도 마찬가지로 N이 4이상인 경우로 제한해줘야 한다. + for i in 4 ... N { + array[i] = array[i - 1] + array[i - 2] + array[i - 3] + } + } + + return array[N] +} + +for k in number { + print(dp(N : k)) +} + diff --git a/sep037/README.md b/sep037/README.md index df688be..6ad5f2c 100644 --- a/sep037/README.md +++ b/sep037/README.md @@ -7,5 +7,7 @@ |:----:|:---------:|:----:|:-----:|:----:| | 1차시 | 2025.04.05 | 구현 | [숫자 정사각형](https://www.acmicpc.net/problem/1051)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/6| | 2차시 | 2025.04.10 | DFS | [트리의 부모 찾기](https://www.acmicpc.net/problem/11725)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/12| -| 3차시 | 2025.04.38 | 정렬 | [접미사 배열](https://www.acmicpc.net/problem/11656)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/13| +| 3차시 | 2025.04.28 | 정렬 | [접미사 배열](https://www.acmicpc.net/problem/11656)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/13| +| 4차시 | 2025.05.11 | DP | [1, 2, 3 더하기](https://www.acmicpc.net/problem/9095)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/21| + ---