import Foundation
func solution(_ k:Int, _ dungeons:[[Int]]) -> Int {
var result = 0
var visited: [Bool] = Array(repeating: false, count: dungeons.count)
func dfs(_ index: Int, _ tired: Int, _ depth: Int) {
var tired = tired
visited[index] = true
tired -= dungeons[index][1]
for i in 0..<dungeons.count {
if !visited[i] && dungeons[i][0] <= tired { dfs(i, tired, depth + 1) }
}
result = max(result, depth)
visited[index] = false
}
for i in 0..<dungeons.count {
if dungeons[i][0] <= k { dfs(i, k, 1) }
}
return result
}
// ------ 방법 2 ------
func solution2(_ k: Int, _ dungeons: [[Int]]) -> Int {
return explore(k, dungeons, 0)
}
private func explore(_ k: Int, _ dungeons: [[Int]], _ depth: Int) -> Int {
return dungeons.map { dungeon in
k >= dungeon[0] ? explore(k - dungeon[1], dungeons.filter { elem in dungeon != elem }, depth + 1) : depth
}.max() ?? depth
}
'프로그래머스 > Lv.2' 카테고리의 다른 글
[Swift] 땅따먹기 (0) | 2023.04.27 |
---|---|
[Swift] 오픈채팅방 (0) | 2023.04.27 |
[Swift] [3차] N진수 게임 (0) | 2023.04.27 |
[Swift] 큰 수 만들기 (0) | 2023.04.27 |
[Swift] 연속 부분 수열 합의 개수 (0) | 2023.04.25 |