Created
March 1, 2023 22:27
-
-
Save renanreismartins/895d11557159dc2a4469cd2a8c78f76c to your computer and use it in GitHub Desktop.
coin change problem
This file contains hidden or 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
object CanSum extends App { | |
def canSum(n: Int, coins: Array[Int]): Boolean = { | |
if (n == 0) return true | |
for (coin <- coins) { | |
val rest = n - coin | |
if (rest >= 0) return canSum(rest, coins) | |
} | |
false | |
} | |
println(canSum(7, Array[Int](7))) | |
println(canSum(7, Array[Int](1))) | |
println(canSum(7, Array[Int](1, 7))) | |
println(canSum(7, Array[Int](1, 2, 3, 8, 7))) | |
println() | |
println(canSum(300, Array[Int](7, 14))) | |
println(canSum(7, Array[Int](8))) | |
println(canSum(7, Array[Int](6, 7))) | |
println(canSum(7, Array[Int](5, 3, 4, 7))) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment