Created
July 15, 2019 16:26
-
-
Save cg4jins/3d9f2200526845d1517252a2b51238fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun solution(N: Int, number: Int): Int { | |
val f = f(N, number) | |
if (f > 8){ | |
return -1 | |
} | |
return f | |
} | |
private var answerMap = mutableMapOf<Int, Int>() | |
init { | |
answerMap[1] = 2 | |
answerMap[11] = 3 | |
answerMap[12] = 4 | |
} | |
private fun f(N: Int, m: Int): Int { | |
if (m == N){ | |
return 1 | |
} | |
if (answerMap.containsKey(m)){ | |
return answerMap[m]!! | |
} | |
var listOfCount = arrayListOf<Int>() | |
for (i in 1 until m){ | |
listOfCount.add(f(N, i) + f(N, m-i)) | |
listOfCount.add(f(N, m + i) - f(N, i)) | |
listOfCount.add(f(N, i) * f(N, m/i)) | |
listOfCount.add(f(N, m * i) / f(N, i)) | |
} | |
answerMap[m] = listOfCount.min() ?: 0 | |
return answerMap[m] ?: 0 | |
} | |
} | |
fun main() { | |
for (i in 1 .. 2){ | |
println("$i=${Solution().solution(5, i)}") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment