SW Test/Programmers
[Programmers] Lv2. ํ๊ฒ ๋๋ฒ(kotlin)
An effort will never betray ๐
2022. 8. 7. 23:54
๋ฐ์ํ
๋ฌธ์
์์
ํ์ด
- DFS ๊ธฐ๋ณธ์ ์ธ ๋ฌธ์ ์ ํ์ผ๋ก ์ฌ๊ท์ break point๋ง ์ ์ง์ ํ๋ฉด ๋ฉ๋๋ค.
class Solution {
var answer = 0
fun getresult(numbers: IntArray, checks: IntArray, target: Int){
if(checks.size == numbers.size){
val sum = numbers.foldIndexed(0){index, acc, i ->
acc + i*checks[index]
}
if(target == sum) answer++
}else{
getresult(numbers, checks + intArrayOf(-1), target)
getresult(numbers, checks + intArrayOf(1), target)
}
}
fun solution(numbers: IntArray, target: Int): Int {
getresult(numbers, intArrayOf(), target)
return answer
}
}
์ฐธ๊ณ
- ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ธ kotlin์ ํน์ฑ์ ๋ง๊ฒ ํจ์๋ฅผ ๋ง์ด ์ตํ์ ๐ฝ
- https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/fold-indexed.html
๋ฐ์ํ