Partitions, Coins, and Others
Coins
The number of ways to obtain a given sum using a given set of coins.
Algorithm:
-
Step 1 (Initialization).
- Create an array
waysof lengthsum + 1, initialized with zeros for all elements. - Set
ways(0) = 1, since there is one way to obtain the sum0(by using no coins).
- Create an array
-
Step 2 (Loop over coins). For each coin in the
coinsarray, perform the following steps:- Iterate over all sums from the current coin's value to
sum(inclusive). - For each sum
i, update the number of ways to obtain it:ways(i) += ways(i - coin), wherecoinis the current coin. This means that if we can obtain the sumi - coin, then adding the current coin gives us the sumi. Therefore, the number of ways to obtain sumiincreases by the number of ways to obtain the sumi - coinfor eachi.
- Iterate over all sums from the current coin's value to
- Return result:
After completing all iterations, return the value
ways(sum), which will contain the number of ways to obtain the target sum.
Example:
Consider the example of computing the number of ways to obtain the sum 4, using coins (1, 2, 3):
- Step 1:
ways = [1, 0, 0, 0, 0],coins = [1, 2, 3],sum = 4. -
Step 2:
-
Iteration 1 (coin = 1):
- For
i = 1:ways(1) += ways(1 - 1) => ways(1) += ways(0) => ways(1) = 1 - For
i = 2:ways(2) += ways(2 - 1) => ways(2) += ways(1) => ways(2) = 1 - For
i = 3:ways(3) += ways(3 - 1) => ways(3) += ways(2) => ways(3) = 1 - For
i = 4:ways(4) += ways(4 - 1) => ways(4) += ways(3) => ways(4) = 1
- For
- The
waysarray after the first iteration:ways = [1, 1, 1, 1, 1] -
Iteration 2 (coin = 2):
- For
i = 2:ways(2) += ways(2 - 2) => ways(2) += ways(0) => ways(2) = 2 - For
i = 3:ways(3) += ways(3 - 2) => ways(3) += ways(1) => ways(3) = 2 - For
i = 4:ways(4) += ways(4 - 2) => ways(4) += ways(2) => ways(4) = 3
- For
- The
waysarray after the second iteration:ways = [1, 1, 2, 2, 3] -
Iteration 3 (coin = 3):
- For
i = 3:ways(3) += ways(3 - 3) => ways(3) += ways(0) => ways(3) = 3 - For
i = 4:ways(4) += ways(4 - 3) => ways(4) += ways(1) => ways(4) = 4
- For
- The
waysarray after the third iteration:ways = [1, 1, 2, 3, 4]
-
Iteration 1 (coin = 1):
- Step 3: Return result: Return
ways(4), which equals4. - Summary: Thus, there are 4 ways to obtain the sum 4, using coins 1, 2, and 3.
Estimation:
-
Time complexity:
- The outer loop iterates over all coins, which is O(n), where n is the number of distinct coins.
- The inner loop iterates from the current coin's value to the given sum (sum). In the worst case, if all coins have the minimum value (e.g., 1), this loop runs sum times.
- Thus, the overall time complexity is O(n * sum).
-
Space complexity:
- The algorithm uses a
waysarray of size (sum + 1) to store the number of ways for each sum from 0 to sum. This requires O(sum) memory. - No other significant data structures are used in the algorithm, so the overall space complexity is O(sum).
- The algorithm uses a
-
Time - O(n * sum)
-
Memory - O(sum)
Code:
def countWays(coins: Array[Int], sum: Int): Long =
val ways = Array.fill(sum + 1)(0L)
ways(0) = 1
for
coin <- coins
i <- coin to sum
do ways(i) += ways(i - coin)
ways(sum)
countWays(Array(1, 2, 5, 10, 20, 50, 100, 200), 200)
// 73682
Partitions
Number of ways to write n as a sum of positive integers.
Code:
def partition(number: Int): BigInt =
val array = new Array[BigInt](number + 1)
array(0) = BigInt(1)
if number >= 1 then array(1) = BigInt(1)
if number >= 2 then array(2) = BigInt(2)
def partitionPart(s: Int, n: Int, pS: BigInt): BigInt =
@tailrec
def loop(k: Int, p: BigInt): BigInt =
val op = k * (3 * k - 1) / 2
if op > n then p
else
val newP =
if (k + 1) % 2 == 0 then p + array(n - op)
else p - array(n - op)
loop(k + s, newP)
loop(s, pS)
(3 to number).foreach: n =>
val p = partitionPart(1, n, BigInt(0))
array(n) = partitionPart(-1, n, p)
array(number)
end partition
partition(1000)
// BigInt("24061467864032622473692149727991")
Links: