From 39e19bce7fbf8a46826ae7d675bdcd3bcc9e50d0 Mon Sep 17 00:00:00 2001 From: ops1111 Date: Mon, 14 Oct 2019 00:28:41 +0700 Subject: [PATCH] Turn CoinChange into a more idiomatic way --- src/main/scala/DynamicProgramming/CoinChange.scala | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main/scala/DynamicProgramming/CoinChange.scala b/src/main/scala/DynamicProgramming/CoinChange.scala index 93501e0..f18ac58 100644 --- a/src/main/scala/DynamicProgramming/CoinChange.scala +++ b/src/main/scala/DynamicProgramming/CoinChange.scala @@ -21,12 +21,11 @@ object CoinChange { combinations(0) = 1 - for (coin <- coins) { - for (i <- coin to money) { - if (i >= coin) { - combinations(i) += combinations(i-coin) - } - } + for { + coin <- coins + i <- coin to money + } { + combinations(i) += combinations(i-coin) } combinations(money)