space optimized solution for coin change
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
The Coin Change problem is a classic algorithmic challenge in computer science and involves finding the minimum number of coins required to make a given amount using a set of denominations. A space-optimized solution seeks to reduce the memory usage while maintaining or improving computational efficiency. This article delves into the space-optimized solution for the Coin Change problem, highlighting its technical peculiarities and providing practical examples.
Problem Description
Given a set of coin denominations and a target amount, the objective is to determine the minimum number of coins needed to sum up to the target amount.
Example
Consider the coin denominations: [1, 2, 5]
and the target amount: 11
. The algorithm should return 3
because the amount 11
can be formed by using the coins [5, 5, 1]
, making 3 coins in total.
Dynamic Programming Approach
The Coin Change problem can be solved via dynamic programming, where we build a solution using previously computed subproblems. The traditional dynamic programming approach for coins utilizes a table to store the solutions to subproblems, allowing the algorithm to calculate the minimum coins required for each amount up to the target amount.
Traditional DP Solution
For an amount N
and coins of denomination d
, use a table dp[]
where dp[i]
holds the minimum number of coins to sum up to i
. The recurrence relation is as follows:
The time complexity is , while the space complexity is because a table that holds the minimum coins for each amount up to N
must be stored.
Space Optimized Solution
While the traditional DP approach is effective, it can further be optimized in terms of space complexity. Instead of storing results for all subproblems, we only keep track of the results for the previous state, drastically reducing memory usage.
Optimized Approach
The optimized approach involves maintaining a single array of size amount+1
, which updates the minimum coins needed for each sub-amount iteratively.
Algorithm
- Initialize an array
dp[]to store the minimum number of coins needed for each value up to the target amount, initializingdp[0] = 0and all other entries to infinity. - Iterate over each coin denomination.
- For every coin, iterate through possible amounts, updating the
dp[]array: • Update the array with:
Example in Python
• Time Complexity: , where is the target amount and is the number of coin denominations. • Space Complexity: due to the use of a single array representing the target amount.
Related reading
- Spanning tree which minimizes the number of vertices connected to multiple edges?
- Spark Counting co-occurrence - Algorithm for efficient multi-pass filtering of huge collections
- Spark What is the time complexity of the connected components algorithm used in GraphX?
- Spatial data structure for finding all points greater than or less than a value in each cartesian dimension
- Spark job running for long for too small data
- Spark Structured Streaming - Limitations? (Source Performance, Unsupported Operations, Spark UI)
- Specialised algorithm to find positive real solutions to quartic equations?
- Speed-efficient classification in Matlab

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.