새소식

인기 검색어

프로그래머스/Lv.2

[Swift] 피로도

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

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.