-
Notifications
You must be signed in to change notification settings - Fork 0
5-sep037 #29
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
base: main
Are you sure you want to change the base?
Conversation
giljihun
left a comment
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.
μ 30λΆλ§μ νΈμ
¨λ€λ DOG GOSUλ€μ κΈλ‘λ.
μ λ μ루μ
ννΈλ μμ λΉμ·νκ² νμκ±°λ μ!
κ·Όλ° μ΄ λ¬Έμ λ μμ νμμ λ§μ§λ§
λΆλ₯λ₯Ό μμ νμμΌλ‘ νκΈ° 보λ€λ,
μ λ λ°±νΈλνΉ λ°©μμ dfsλλμ΄ κ°νλ€κ³ μκ°νμ΄μ!
λ¬Όλ‘ μ¬κ· λ§μ νμ κ±Έ 보λ κ°μ μκ°μ νμ
¨μ κ² κ°μ§λ§, μ΄ λΆλΆ λ€μ 체ν¬ν΄λ΄λ μ’μ κ² κ°μ΅λλ€
ν 릴리~

μ λ ₯λΆλ§ 첨λΆν©λλ€!
import Foundation
/*
μμλλ‘ λ μ§λ₯Ό λνμλ NμΌμ΄ λμ€λ κ²½μ°μ μ, κ°κ²©μ ꡬν΄μ
μ΅λκ° κ°±μ ?
*/
// μ
λ ₯λΆ
guard let N = Int(readLine() ?? ""),
N >= 1, N <= 15 else { exit(0) }
var schedule: [(Int, Int)] = []
(0..<N).forEach { _ in
guard let input = readLine()?.split(separator: " "),
input.count == 2,
let day = Int(input[0]),
let time = Int(input[1]) else { exit(0) }
schedule.append((day, time))
}| import Foundation | ||
|
|
||
| let N = Int(readLine()!)! | ||
| var profitFromWork : [(Int, Int)] = [] |
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.
λ³μλͺ μλ²½ν©λλ€. ^^..
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.
γ γ γ γ γ γ γ γ γ γ γ γ γ γ±γ ν©λλ€ ! λ λ©μ§ λ³μλͺ μ§κ³ μΆμλ° μ λ κ² μ§κ΄μ μΈ κ² λ νΈν΄μ μ¬μ€ .
|
@giljihun κ°λλ¬Έμ μ κΈΈμ¨ |
|
μΆκ°μ μΌλ‘.. μ λ΅μ 보λκΉ |
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.
30λΆ κ±Έλ¦¬μ κ±° 리μ€ν..
μ λ 1μκ°μ νμ©λκ²Όμ΅λλ€..
μ λ λ€λ₯ΈλΆλ€μ²λΌ dfs μΌμ΅λλ€.
μ²μμλ μ΄κ±Έ λ€μμλΆν° κΊΌλ΄λ©΄μ 보면 μ’ λ€λ₯ΌκΉ?ν΄μtmpArrμreverseλ‘ λ€μ§κ³ νλ¦°νΈμ°μ΄λ³΄λ€κ° κ²°κ΅ μ€ν¨νκ³ DFSλ‘..
μλμ½λ
- ν΄μ¬μνμΈ κ²λ€ λ€ κ±Έλ¬μ£ΌκΈ°
- dfsλ‘ ν΄λΉλ μ§μμ νμλ₯Ό μμνμλμ μνμλλ₯Ό λλ €μ£Όλ©΄μ μ΅λκ° μ°ΎκΈ°
- dayκ° Nλ³΄λ€ μ»€μ§λ©΄ μ΄μ λ€ λμμΌλ μ’ λ£
μ΄μ§ λ€λ₯Έμ
- μκ°μ μ‘°κΈμ΄λΌλ μ€μ΄κ³ μ νλ°ν΄λλ©΄μ
endκ° ν΄μ¬μΌ μ΄νμ μλ μ λ€μ μ μ΄μ ν보ꡰμμ μμ μ£Όκ³ μμνμ΄μ. - index λ₯Ό 1λΆν°μμνκ² ν΄μ£Όλ €κ³ μ‘°μ μ ν΄μ€¬μμ΅λλ€ ! 6μΌμμμν΄μ ν루면 start - 6 , end - 6
typealias Talk = (start: Int, end: Int, profit: Int)
func solution(_ N: Int, _ arr: [(Int, Int)]) -> Int {
var answer = 0
var tmpArr = [Talk]()
func dfs(day: Int, profit: Int) {
if day > N {
answer = max(answer, profit)
return
}
if let talk = tmpArr.first(where: { $0.start == day }) {
dfs(day: talk.end + 1, profit: profit + talk.profit)
}
dfs(day: day + 1, profit: profit)
}
for (idx, item) in arr.enumerated() {
let start = idx + 1
let end = start + item.0 - 1
let profit = item.1
// μΌλ¨ μμ±μΌ κΈ°μ€ ν΄μ¬μνμΈκ±° λ€ κ±Έλ¬μ£ΌκΈ°
if end <= N {
tmpArr.append((start: start, end: end, profit: profit))
}
}
dfs(day: 1, profit: 0)
return answer
}μμ¬μ΄μ
if let talk = tmpArr.first(where: { $0.start == day }) {
dfs(day: talk.end + 1, profit: profit + talk.profit)
}μ΄κ² tmpArr κ΅³μ΄ κ±Έλ¬μ£Όμ§ μμμΌλ©΄ 무쑰건 startκ° μμκ±°λΌ κ·Έλ₯ if let κ΅³μ΄ νμ μμκ³ κ°μ μΈλν μ¨λ λμ λ―, νμκ° λ§€μΌ μμνλκΉ
alstjr7437
left a comment
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.
λκ° dpλ‘ λ κ² κ°μλ° λμ ν μμ΄λμ΄κ° μκ°μ΄ μλμ μΈν°λ·μ μ°Έκ³ ν΄μ
dpλ‘ νμ΄λ΄€μ΅λλ€~~
let N = Int(readLine()!)!
var schedule = [(0,0)]
for _ in 0..<N {
let input = readLine()!.split(separator: " ").map{ Int($0)! }
schedule.append((input[0], input[1]))
}
var dp = Array(repeating: 0, count: N+2)
for i in 1..<N+1 {
// λ€μλ λ‘ λμ΄κ°λλ μ΅λ 보μμ μ΄μ΄λκ°μΌν¨
if dp[i] > dp[i+1] { dp[i+1] = dp[i] }
// ν΄μ¬λ λ³΄λ€ μ«μκ° λμΌλ©΄ κΈμ¬κ° μμ±μμ§λ―λ‘ continue
guard i+schedule[i].0 <= N+1 else { continue }
dp[i + schedule[i].0] = max(
schedule[i].1 + dp[i], // μ€λ μλ΄νκ³ λ³΄μ λ°λ κ²½μ°
dp[i + schedule[i].0] // κΈ°μ‘΄μ κ·Έ λ κΉμ§ μμΈ λ³΄μ
)
}
print(dp.max()!)| if day + profitFromWork[day].0 <= N { | ||
| workTime(day: day + profitFromWork[day].0, profit: profit + profitFromWork[day].1) | ||
| } | ||
| workTime(day: day + 1, profit: profit) | ||
| } |
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.
μΉ μ¬κ·ν¨μλ₯Ό μ΄μ©ν΄μ μ μ λ³μμ maxλ₯Ό μ¬μ©ν΄μ ꡬννμ ¨λ€μ!!
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.
μ λ ₯ κ° μ΅λμΉκ° 15λΌμ μννλ©΄ λκ² λ€ μκ°μ νλλ° μ¬κ·λ‘ μ ꡬννμ λ―!
| | 2μ°¨μ | 2025.04.10 | DFS | [νΈλ¦¬μ λΆλͺ¨ μ°ΎκΈ°](https://www.acmicpc.net/problem/11725)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/12| | ||
| | 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| | ||
| | 5μ°¨μ | 2025.05.24 | BruteForce | [ν΄μ¬](https://www.acmicpc.net/problem/14501)|https://github.com/AlgoLeadMe/AlgoLeadMe-14/pull/29| |
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.
dfsλ‘ νμλλ° μ μ¬κΈ° λΈλ£¨νΈν¬μ€λ‘ λ£μμ΄μ λ§μΉ΄λ‘λμ¨ γ γ
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.
ν μ΄κ² λμΌ
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.
κΈ°μ΅ μλ¨ ;;
| let (time, profit) = (input[0], input[1]) | ||
| profitFromWork.append((time, profit)) |
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.
μ΄λ°μμΌλ‘ λ΄μΌμ ¨κ΅°μ! time, profitμ΄ μ€μ μ½λμμ μ°μ΄μ§μμμ
profitFromWork.append((input[0], input[1]))
μ λΌλ©΄ κ°κ²°νκ² μ΄λ κ² νμ λ―! κ·ΈμΉλ§ λ§μΉ΄λ‘λ λ°©μμ΄ λ μμ ν κ² κ°μμ. κΌΌκΌΌν λ§μΉ΄λ‘λμ¨.
| if day + profitFromWork[day].0 <= N { | ||
| workTime(day: day + profitFromWork[day].0, profit: profit + profitFromWork[day].1) | ||
| } | ||
| workTime(day: day + 1, profit: profit) | ||
| } |
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.
μ λ ₯ κ° μ΅λμΉκ° 15λΌμ μννλ©΄ λκ² λ€ μκ°μ νλλ° μ¬κ·λ‘ μ ꡬννμ λ―!
#29
π λ¬Έμ λ§ν¬
ν΄μ¬
μ·¨μ§νμ§λ μμλλ° ν΄μ¬νκ³ μΆλ€
βοΈ μμλ μκ°
30λΆ
β¨ μλ μ½λ
μΌλ¨ μ²μμ μΌμ λΉ κ°μ₯ λ§μ΄ λ² μ μλ μͺ½μΌλ‘ μκ°νλ€κ°
λ€μ μκ°ν΄λ³΄λ κ·Έκ²λ μΌμκ° μ λ§μΌλ©΄ μλ κ² κ°μμ
μμ νκ² μ²« λ λΆν° μΌμ λλ €κ°λ©° ꡬνμ΄μ
ν¨μ (λ μ§, μμ΅) {
if λ μ§κ° μ΄ μΌ μλ³΄λ€ ν¬λ©΄
μ΅λ μμ΅ κ°±μ
λ
if ν΄λΉ μΌ μλ₯Ό λνλλ°λ μ΄ μΌ μλ³΄λ€ μμΌλ©΄
// μλ΄ μ§ν
μλ΄μ νμ§ μμΌλ©΄
κ·Έλ₯ λ μ§μ +1
π μ΄λ €μ λ λ΄μ©
μ¬κ· μκ°μ΄ μ μ λμ . . π₯Ή