SW Test/Programmers

[Programmers] Lv2. ํ”ผ๋กœ๋„(kotlin)

An effort will never betray ๐Ÿ˜Ž 2022. 10. 22. 17:07
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/87946

 

 

์˜ˆ์‹œ

https://school.programmers.co.kr/learn/courses/30/lessons/87946

 

 

 

ํ’€์ด

  • ๋ฐฑํŠธ๋ž˜ํ‚น ๊ธฐ๋ฒ•์„ ์ด์šฉํ•˜๋ฉด ์†์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
import kotlin.math.*

class Solution {
    var answer = -1
    
    fun getResult(k: Int, dungeons: Array<IntArray>, check: List<Boolean>){
        var flag = false
        for(i in 0 until check.size){
            if(!check[i] && k >= dungeons[i][0]){
                flag = true
                getResult(k-dungeons[i][1], dungeons, check.toMutableList().apply{ this[i] = true })
            }
        }
        
        if(!flag){
            answer = max(answer, check.count{it})
        }
    }
    
    fun solution(k: Int, dungeons: Array<IntArray>): Int {
        getResult(k, dungeons, List(dungeons.size){false})
        return answer
    }
}

 

 

 

์ฐธ๊ณ 

  • ํ•จ์ˆ˜ํ˜• ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์ธ kotlin์˜ ํŠน์„ฑ์— ๋งž๊ฒŒ ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ์ตํžˆ์ž ๐Ÿ˜ฝ
๋ฐ˜์‘ํ˜•