-
Notifications
You must be signed in to change notification settings - Fork 0
4-sep037 #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
4-sep037 #21
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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] = [] | ||
|
|
||
| for _ in 0 ..< N { | ||
| number.append(Int(readLine()!)!) | ||
| } | ||
|
Comment on lines
+12
to
+16
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. κΆκΈνμ μ΄ λκ°μ§κ° μμ΄μ.
|
||
|
|
||
| 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ λ μ²μμ
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ λ
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. μ΄λ κ² λ°λ‘ λΉΌλμ ¨κ΅°μ. μ λ€ 1λΆν° λ€ λλ Έλλ°.. μ μμ§ λ©μμ΅λλ€. |
||
|
|
||
| if N >= 4 { // μ¬κΈ°λ λ§μ°¬κ°μ§λ‘ Nμ΄ 4μ΄μμΈ κ²½μ°λ‘ μ νν΄μ€μΌ νλ€. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @YooGyeongMo μ λ ₯ κ° max 11λκ±° μΉΌκ°μ΄ μΊμΉνκ³ μ μ©νμ κ±° λ³΄κ³ μ λ λ¬Έμ μ’ μ λλ‘ μ½μ΄μΌκ² λ€λ μκ°μ νμ΅λλ€. κ³ λ§μμ! |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
λ¬Έμ μμλ Tλ‘ μ£Όμ΄μ Έμ νκ°λ Έλ€μ γ·γ·γ·γ·