Getting started
Sorting
Bubble sort
Algorithm complexity
Graphs
Dynamic programming
Kruskals algorithm
Greedy algorithms
Searching
Big O notation
Bellman-For Algorithm
Merge Sort
Binary Search Trees
Trees
Insertion Sort
Hash Functions
Traveling Salesman
Substring Search
Dijkstras Algorithm
Floyd-Warshall Algorithm
Breadth-First Search
Bucket Sort
Quicksort
Depth-First Search
Knapsack Problem
Counting Sort
Cycle Sort
Heap Sort
Prims Algorithm
Matrix Exponentiation
Pigeonhole Sort
Radix Sort
Equation Solving
Odd-Even Sort
Pseudocode
Catalan Number Algorithm
Integer Partition Algorithm
A* Pathfinding
Shell Sort
Selection Sort
Pancake Sort
Longest Common Subsequence
Longest Increasing Subsequence
Maximum Path Sum Algorithm
Maxiumum Subarray Algorithm
Dynamic Time Warping
Pascal Triangle
Line drawing
Shortest Common Supersequence
Sliding Window Algorithm
Application of greedy techniqe
Online algorithms
Fast Fourier Transform
A* Path-finding Algorithm
Check if tree is BST
Binary tree traversal
Lowest common ancestor of a binary tree
Graph Traversal
Minimum Vertex Cover
Multi-threaded Algorithms
Print MxN matrix in square wise
Check two strings are anagrams
Edit Distance Dynamic Algorithm
Applications of Dynamic Programming
Knuth Moriss Pratt
Contributors

Change-making problem

suggest change

Given a money system, is it possible to give an amount of coins and how to find a minimal set of coins corresponding to this amount.

Canonical money systems. For some money system, like the ones we use in the real life, the “intuitive” solution works perfectly. For example, if the different euro coins and bills (excluding cents) are 1€, 2€, 5€, 10€, giving the highest coin or bill until we reach the amount and repeating this procedure will lead to the minimal set of coins.

We can do that recursively with OCaml :

(* assuming the money system is sorted in decreasing order *)
let change_make money_system amount =
  let rec loop given amount =
    if amount = 0 then given
    else 
      (* we find the first value smaller or equal to the remaining amount *)
      let coin = List.find ((>=) amount) money_system in
      loop (coin::given) (amount - coin)
  in loop [] amount

These systems are made so that change-making is easy. The problem gets harder when it comes to arbitrary money system.

General case. How to give 99€ with coins of 10€, 7€ and 5€? Here, giving coins of 10€ until we are left with 9€ leads obviously to no solution. Worse than that a solution may not exist. This problem is in fact np-hard, but acceptable solutions mixing greediness and memoization exist. The idea is to explore all the possibilies and pick the one with the minimal number of coins.

To give an amount X > 0, we choose a piece P in the money system, and then solve the sub-problem corresponding to X-P. We try this for all the pieces of the system. The solution, if it exists, is then the smallest path that led to 0.

Here an OCaml recursive function corresponding to this method. It returns None, if no solution exists.

(* option utilities *)
let optmin x y =
  match x,y with
  | None,a | a,None -> a
  | Some x, Some y-> Some (min x y)

let optsucc = function
  | Some x -> Some (x+1)
  | None -> None

(* Change-making problem*)
let change_make money_system amount =
  let rec loop n =
    let onepiece acc piece =
      match n - piece with
      | 0 -> (*problem solved with one coin*) 
             Some 1
      | x -> if x < 0 then 
               (*we don't reach 0, we discard this solution*)
               None
             else
               (*we search the smallest path different to None with the remaining pieces*) 
               optmin (optsucc (loop x)) acc
    in
    (*we call onepiece forall the pieces*)
    List.fold_left onepiece None money_system
  in loop amount

Note: We can remark that this procedure may compute several times the change set for the same value. In practice, using memoization to avoid these repetitions leads to faster (way faster) results.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents