coin change
space optimization
dynamic programming
algorithm
computational efficiency

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.

Practice algorithms

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:

dp[i]=min(dp[i],1+dp[icoin])for all coinsdp[i] = \min(dp[i], 1 + dp[i - coin]) \quad \text{for all coins}

The time complexity is O(N×len(coins))O(N \times \text{{len(coins)}}), while the space complexity is O(N)O(N) 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

  1. Initialize an array dp[] to store the minimum number of coins needed for each value up to the target amount, initializing dp[0] = 0 and all other entries to infinity.
  2. Iterate over each coin denomination.
  3. For every coin, iterate through possible amounts, updating the dp[] array: • Update the array with: dp[i]=min(dp[i],dp[icoin]+1)dp[i] = \min(dp[i], dp[i - coin] + 1)

Example in Python

Time Complexity: O(N×k)O(N \times k), where NN is the target amount and kk is the number of coin denominations. • Space Complexity: O(N)O(N) due to the use of a single array representing the target amount.


Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice algorithms

All Rights Reserved.