Guest User

Untitled

a guest
Jun 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. import kotlin.math.min
  2.  
  3. val possibleOperations = arrayOf(
  4. { i: Int -> i + 1 },
  5. { i: Int -> i * 2 },
  6. { i: Int -> i * 3 } )
  7.  
  8. fun countOperationsNumber(n: Int): Int {
  9. val operationsCount = Array(n) { Int.MAX_VALUE }
  10. operationsCount[0] = 0
  11. for (number in 1 until n)
  12. for (operation in possibleOperations) {
  13. val nextNumber = operation(number)
  14. if (nextNumber <= n)
  15. operationsCount[nextNumber - 1] = min(
  16. operationsCount[nextNumber - 1],
  17. operationsCount[number - 1] + 1)
  18. }
  19.  
  20. return operationsCount[n - 1]
  21. }
  22.  
  23. fun main(args: Array<String>) {
  24. println(countOperationsNumber(1))
  25. println(countOperationsNumber(2))
  26. println(countOperationsNumber(3))
  27. println(countOperationsNumber(10))
  28. println(countOperationsNumber(99))
  29. }
Add Comment
Please, Sign In to add comment