Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import kotlin.math.min
- val possibleOperations = arrayOf(
- { i: Int -> i + 1 },
- { i: Int -> i * 2 },
- { i: Int -> i * 3 } )
- fun countOperationsNumber(n: Int): Int {
- val operationsCount = Array(n) { Int.MAX_VALUE }
- operationsCount[0] = 0
- for (number in 1 until n)
- for (operation in possibleOperations) {
- val nextNumber = operation(number)
- if (nextNumber <= n)
- operationsCount[nextNumber - 1] = min(
- operationsCount[nextNumber - 1],
- operationsCount[number - 1] + 1)
- }
- return operationsCount[n - 1]
- }
- fun main(args: Array<String>) {
- println(countOperationsNumber(1))
- println(countOperationsNumber(2))
- println(countOperationsNumber(3))
- println(countOperationsNumber(10))
- println(countOperationsNumber(99))
- }
Add Comment
Please, Sign In to add comment