|
ADS
Algorithms and Data Structures
|
The change with the fewest coins (introduction of chapter 14): the greedy choice takes the largest coin that fits, and is optimal for coins of 50, 10, 5 and 1 cents, but not for coins of 11, 5 and 1, where it pays 15 with five coins instead of three of 5. More...
#include "ADS/Contract.h"#include <algorithm>#include <cstddef>#include <limits>#include <numeric>#include <optional>#include <vector>#include "ADS/optimization/impl/CoinChange.tpp"Go to the source code of this file.
Namespaces | |
| namespace | ADS |
Functions | |
| std::optional< std::vector< long > > | ADS::greedyChange (std::vector< long > coins, long c) |
| the coins taken by the greedy choice, the coins given in any order; nothing if it cannot pay | |
| std::optional< long > | ADS::optimalChange (const std::vector< long > &coins, long c) |
| the fewest coins that pay \( c \), nothing if none | |
The change with the fewest coins (introduction of chapter 14): the greedy choice takes the largest coin that fits, and is optimal for coins of 50, 10, 5 and 1 cents, but not for coins of 11, 5 and 1, where it pays 15 with five coins instead of three of 5.
optimalChange() is the dynamic programming, the fewest coins \( N[ c ] \) of each amount \( c \), \( N[ c ] = 1 + \min_{d \le c} N[ c - d ] \), \( O( n C ) \) for \( n \) coins and the amount \( C \).
The contracts [see Contract.h]: the coins are positive and the amount is not negative; the change of greedyChange() sums to the amount, and optimalChange() uses at most \( c \) coins. The operations are defined in CoinChange.tpp.