Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions sep037/DP/BOJ_9095.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// main.swift
// 9095
//
// Created by Seungeun Park on 5/11/25.
//

import Foundation

let N = Int(readLine()!)!

var number : [Int] = []
Comment on lines +9 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λ¬Έμ œμ—μ„œλŠ” T둜 μ£Όμ–΄μ Έμ„œ ν–ˆκ°ˆλ Έλ„€μš” γ„·γ„·γ„·γ„·


for _ in 0 ..< N {
number.append(Int(readLine()!)!)
}
Comment on lines +12 to +16
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κΆκΈˆν•œμ μ΄ 두가지가 μžˆμ–΄μš”.

  1. λ”°λ‘œ number 배열을 λ‘” μ΄μœ κ°€ μžˆμ„κΉŒμš”? + λ°°μ—΄ 넀이밍은 's'λ₯Ό 뒀에 λΆ™μ—¬μ£ΌλŠ”κ²Œ κ΄€ν–‰μ΄μ˜΅λ‹ˆλ‹€.
  2. μ–΄λ–¨ 땐 배열을 var number = [Int]()둜 μ„ μ–Έν•˜μ‹œκ³  μ΄λ²ˆμ—” var number: [Int] = []와 같이 μ„ μ–Έν•˜μ…¨λŠ”λ° 기쀀이 μžˆλ‚˜μš”?


func dp(N : Int) -> Int {
//var array = [Int](repeating: 0, count: N + 1)
// μ²˜μŒμ— 생각 μ•ˆ ν•˜κ³  N개 λ§Œλ“€μ—ˆλ‹€κ°€ N에 2λ‚˜ 1 같은 게 λ“€μ–΄μ˜¬ 수 μžˆλŠ” κ±Έ λŒ€μ‘ μ•ˆ ν–ˆλ‹€.
// λ§Œμ•½ 1이 듀어왔을 경우 μ•„λž˜ μ΄ˆκΈ°ν™”λŠ” boundλ₯Ό λ„˜μ–΄μ„œκ²Œ λœλ‹€. κ·Έλ ‡λ‹€λ©΄ μ΅œμ†Œ 배열을 4둜 λ§Œλ“€μ–΄μ£Όλ©΄ 됨 !
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

λŸ°νƒ€μž„μ˜€λ₯˜μ— λŒ€ν•œ 처리λ₯Ό μž˜ν•˜μ…¨λ„€μš”.
μ €λŠ” μ•„λž˜μ²˜λŸΌ λ‹¨μˆœν•˜κ²Œ ν•΄μ„œ μ‹œμž‘ν–ˆμ–΄μš”~

    if n == 1 {
        answer.append(1)
        return
    }
    
    if n == 2 {
        answer.append(2)
        return
    }
    
    if n == 3 {
        answer.append(4)
        return
    }

var array = [Int](repeating: 0, count: max(N + 1, 4))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저도 μ²˜μŒμ— count : N + 1둜 ν–ˆλ‹€κ°€ λŸ°νƒ€μž„μ—λŸ¬μ— λ‹Ήν–ˆλ„€μš”..!
max둜 μ΅œμ†Œλ°°μ—΄ ν•΄μ£ΌλŠ”κ±° μ‹ μ„ ν•˜λ„€μš”! πŸ‘ 저도 λ‹€μŒλ²ˆμ— μ¨λ¨Ήκ² μŠ΅λ‹ˆλ‹€.
이 λ¬Έμ œλŠ” μ΅œλŒ€ N이 11 밖에 μ•ˆλΌμ„œ count:12둜 ν•΄μ£ΌλŠ”κ²ƒλ„ 쒋은 것 κ°™μ•„μš”!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ €λŠ” var dp = Array(repeating: 0, count: N + 1)으둜 λ‹€ν–‰νžˆ? λŸ°νƒ€μž„μ„ ν”Όν–ˆλ„€μš” γ„·γ„·γ„·

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

였우 λ§ˆμΉ΄λ‘œλ‹ˆ.... κ³ μˆ˜λ‹€.... μ‘μš©ν•˜κΈ° 쒋은 μ½”λ“œλ„€μš”.


array[1] = 1
array[2] = 2
array[3] = 4 // 3κΉŒμ§€λŠ” 자기 μžμ‹ μ„ κ°€μ§€κ³  있기 λ•Œλ¬Έμ— μ΄ˆκΈ°ν™” ν•΄μ€˜μ•Ό 함
Comment on lines +19 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ €λŠ” 보톡 DPλ‚˜ λ°°μ—΄μ˜ 0-based index ν˜Ήμ€ 1-based index ν• λ•Œ μž…λ ₯κ°’μ˜ μ œν•œμ„ λ³΄λŠ” 편인데, μ €λŠ” κ·Έλƒ₯ λ°”λ‘œ 1~10κΉŒμ§€λ§Œ λ‚˜μ˜€λ©΄ λ˜κ² λ‹€λΌκ³  생각을 ν–ˆμŠ΅λ‹ˆλ‹€.

κ·Έλž˜μ„œ λͺ¨λ“ λ°°μ—΄μ„ κ·Έλƒ₯ ν•œλ²ˆμ— μ΄ˆκΈ°ν™”ν•˜κ³  μ›ν•˜λŠ” 쑰건을 λ±‰μ–΄μ£Όμž dp + μ•½κ°„ ν•΄μ‹± λŠλ‚ŒμœΌλ‘œ μ ‘κ·Όν•œκ±°κ°™μ•„μš”.

func solution() {
    let testCase = Int(readLine()!)!
    var dp = Array(repeating:0, count:11)
    dp[1] = 1
    dp[2] = 2
    dp[3] = 4
    
    for i in 4..<11 {
        dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
    }
    

κΈ€λ‘œλ¦¬μ˜ μ½”λ“œλŠ” μ €μ˜ μ½”λ“œμ— λΉ„ν•΄μ„œ μœ λ™μ„±μ΄ ν™•μ‹€νžˆ μ’‹λ‹€κ³  νŒλ‹¨ν–ˆμŠ΅λ‹ˆλ‹€. ν™•μ‹€ν•œ μ΄ˆκΈ°ν™”κ°’ μ΄ˆκΈ°κ°’μ„ λ°”λ‘œ 뱉어주기 μœ„ν•΄μ„œ 1,2,3 λ¨Όμ € 배열에 λ„£κ³  좔후에 μΆ”κ°€λ˜λŠ” 값듀을 μΈλ±μ‹±μœΌλ‘œ λ„£μ–΄μ£ΌλŠ” νŒλ‹¨μ„ν•˜μ…¨λŠ”λ° λ„ˆλ¬΄ μ’‹μ€κ±°κ°™μ•„μš”.

이 λ¬Έμ œμ—μ„œλŠ” κΈ€λ‘œλ¦¬μ˜ μž…λ ₯ 좜λ ₯방식이 더 λ¬Έμ œμ— μ΅œμ ν™”λ˜μ–΄μžˆμ—ˆλ˜κ²ƒκ°™μŠ΅λ‹ˆλ‹€ ! μ•„μ£Ό 쒋은 μ½”λ“œλ„€μš” !

Comment on lines +24 to +26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ΄λ ‡κ²Œ λ”°λ‘œ λΉΌλ‘μ…¨κ΅°μš”. μ „ λ‹€ 1λΆ€ν„° λ‹€ λŒλ ΈλŠ”λ°.. μ „ 아직 λ©€μ—ˆμŠ΅λ‹ˆλ‹€.


if N >= 4 { // 여기도 λ§ˆμ°¬κ°€μ§€λ‘œ N이 4이상인 경우둜 μ œν•œν•΄μ€˜μ•Ό ν•œλ‹€.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ΄λŸ΄λ•Œ guardκ°™μ€κ±°λ‘œ μ œν•œλ‘μ‹œλ©΄ λ°”λ‘œ λ°˜λ³΅λ¬Έλ„ μ‹œμž‘ν•  수 있고 더 쒋을 것 κ°™μ•„μš”!
저도 μ΅μˆ™ν•˜μ§€ μ•Šμ•„μ„œ μ–΅μ§€λ‘œλΌλ„ μ¨λ³΄λ €κ΅¬μš”..! πŸ‘

guard n > 3 else {
        print(arr[n])
        return
    }
    for i in 4 ... n {
        arr[i] = arr[i-1] + arr[i-2] + arr[i-3]
    }

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))
}

Comment on lines +28 to +40
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

κΈ€λ‘œλ¦¬λ„ μˆ¨κ²¨μ§„ ADA의 고수일 μˆ˜λ„ ν•¨μˆ˜ λ‚˜λˆ„λŠ”κ²Œ λ„ˆλ¬΄ λ³΄κΈ°μ’‹μ•„μš”. μ„±λŠ₯에 μ‹ κ²½μ“΄ μ½”λ“œμž…λ‹ˆλ‹€. 캐싱 ν•΄μ‹± ꡬ쑰 λ„κ³ μžˆμœΌλ‚˜, 좔후에 μž…λ ₯을 strictν•˜κ²Œ μ€€λ‹€λ©΄ μ €λŠ” μ•„λ§ˆλ„ ν‹€λ¦° μ½”λ“œμ΄μ§€ μ•Šμ„κ°€ μ‹Άλ„€μš”.

import Foundation

func solution() {
    let testCase = Int(readLine()!)!
    var dp = Array(repeating:0, count:11)
    dp[1] = 1
    dp[2] = 2
    dp[3] = 4
    
    for i in 4..<11 {
        dp[i] = dp[i-1] + dp[i-2] + dp[i-3]
    }
    
    for _ in 0..<testCase {
        let n = Int(readLine()!)!
        print(dp[n])
    }
}

solution()

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@YooGyeongMo μž…λ ₯ κ°’ max 11λ‘”κ±° 칼같이 μΊμΉ˜ν•˜κ³  μ μš©ν•˜μ‹ κ±° 보고 저도 문제 μ’€ μ œλŒ€λ‘œ μ½μ–΄μ•Όκ² λ‹€λŠ” 생각을 ν–ˆμŠ΅λ‹ˆλ‹€. κ³ λ§ˆμ›Œμš”!

4 changes: 3 additions & 1 deletion sep037/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|

---