Collect all coins in minimum number of steps
Last Updated :
10 Apr, 2023
Given many stacks of coins which are arranged adjacently. We need to collect all these coins in the minimum number of steps where in one step we can collect one horizontal line of coins or vertical line of coins and collected coins should be continuous.
Examples :
Input : height[] = [2 1 2 5 1]
Each value of this array corresponds to
the height of stack that is we are given
five stack of coins, where in first stack
2 coins are there then in second stack
1 coin is there and so on.
Output : 4
We can collect all above coins in 4 steps
which are shown in below diagram.
Each step is shown by different color.
First, we have collected last horizontal
line of coins after which stacks remains
as [1 0 1 4 0] after that, another horizontal
line of coins is collected from stack 3
and 4 then a vertical line from stack 4
and at the end a horizontal line from
stack 1. Total steps are 4.
We can solve this problem using divide and conquer method. We can see that it is always beneficial to remove horizontal lines from below. Suppose we are working on stacks from l index to r index in a recursion step, each time we will choose minimum height, remove those many horizontal lines after which stack will be broken into two parts, l to minimum and minimum +1 till r and we will call recursively in those subarrays. Another thing is we can also collect coins using vertical lines so we will choose minimum between the result of recursive calls and (r - l) because using (r - l) vertical lines we can always collect all coins.
As each time we are calling each subarray and finding minimum of that, total time complexity of the solution will be O(N2)
C++
// C++ program to find minimum number of
// steps to collect stack of coins
#include <bits/stdc++.h>
using namespace std;
// recursive method to collect coins from
// height array l to r, with height h already
// collected
int minStepsRecur(int height[], int l, int r, int h)
{
// if l is more than r, no steps needed
if (l >= r)
return 0;
// loop over heights to get minimum height
// index
int m = l;
for (int i = l; i < r; i++)
if (height[i] < height[m])
m = i;
/* choose minimum from,
1) collecting coins using all vertical
lines (total r - l)
2) collecting coins using lower horizontal
lines and recursively on left and right
segments */
return min(r - l,
minStepsRecur(height, l, m, height[m]) +
minStepsRecur(height, m + 1, r, height[m]) +
height[m] - h);
}
// method returns minimum number of step to
// collect coin from stack, with height in
// height[] array
int minSteps(int height[], int N)
{
return minStepsRecur(height, 0, N, 0);
}
// Driver code to test above methods
int main()
{
int height[] = { 2, 1, 2, 5, 1 };
int N = sizeof(height) / sizeof(int);
cout << minSteps(height, N) << endl;
return 0;
}
Java
// Java Code to Collect all coins in
// minimum number of steps
import java.util.*;
class GFG {
// recursive method to collect coins from
// height array l to r, with height h already
// collected
public static int minStepsRecur(int height[], int l,
int r, int h)
{
// if l is more than r, no steps needed
if (l >= r)
return 0;
// loop over heights to get minimum height
// index
int m = l;
for (int i = l; i < r; i++)
if (height[i] < height[m])
m = i;
/* choose minimum from,
1) collecting coins using all vertical
lines (total r - l)
2) collecting coins using lower horizontal
lines and recursively on left and right
segments */
return Math.min(r - l,
minStepsRecur(height, l, m, height[m]) +
minStepsRecur(height, m + 1, r, height[m]) +
height[m] - h);
}
// method returns minimum number of step to
// collect coin from stack, with height in
// height[] array
public static int minSteps(int height[], int N)
{
return minStepsRecur(height, 0, N, 0);
}
/* Driver program to test above function */
public static void main(String[] args)
{
int height[] = { 2, 1, 2, 5, 1 };
int N = height.length;
System.out.println(minSteps(height, N));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python 3
# Python 3 program to find
# minimum number of steps
# to collect stack of coins
# recursive method to collect
# coins from height array l to
# r, with height h already
# collected
def minStepsRecur(height, l, r, h):
# if l is more than r,
# no steps needed
if l >= r:
return 0;
# loop over heights to
# get minimum height index
m = l
for i in range(l, r):
if height[i] < height[m]:
m = i
# choose minimum from,
# 1) collecting coins using
# all vertical lines (total r - l)
# 2) collecting coins using
# lower horizontal lines and
# recursively on left and
# right segments
return min(r - l,
minStepsRecur(height, l, m, height[m]) +
minStepsRecur(height, m + 1, r, height[m]) +
height[m] - h)
# method returns minimum number
# of step to collect coin from
# stack, with height in height[] array
def minSteps(height, N):
return minStepsRecur(height, 0, N, 0)
# Driver code
height = [ 2, 1, 2, 5, 1 ]
N = len(height)
print(minSteps(height, N))
# This code is contributed
# by ChitraNayal
C#
// C# Code to Collect all coins in
// minimum number of steps
using System;
class GFG {
// recursive method to collect coins from
// height array l to r, with height h already
// collected
public static int minStepsRecur(int[] height, int l,
int r, int h)
{
// if l is more than r, no steps needed
if (l >= r)
return 0;
// loop over heights to
// get minimum height index
int m = l;
for (int i = l; i < r; i++)
if (height[i] < height[m])
m = i;
/* choose minimum from,
1) collecting coins using all vertical
lines (total r - l)
2) collecting coins using lower horizontal
lines and recursively on left and right
segments */
return Math.Min(r - l,
minStepsRecur(height, l, m, height[m]) +
minStepsRecur(height, m + 1, r, height[m]) +
height[m] - h);
}
// method returns minimum number of step to
// collect coin from stack, with height in
// height[] array
public static int minSteps(int[] height, int N)
{
return minStepsRecur(height, 0, N, 0);
}
/* Driver program to test above function */
public static void Main()
{
int[] height = { 2, 1, 2, 5, 1 };
int N = height.Length;
Console.Write(minSteps(height, N));
}
}
// This code is contributed by nitin mittal
PHP
<?php
// PHP program to find minimum number of
// steps to collect stack of coins
// recursive method to collect
// coins from height array l to
// r, with height h already
// collected
function minStepsRecur($height, $l,
$r, $h)
{
// if l is more than r,
// no steps needed
if ($l >= $r)
return 0;
// loop over heights to
// get minimum height
// index
$m = $l;
for ($i = $l; $i < $r; $i++)
if ($height[$i] < $height[$m])
$m = $i;
/* choose minimum from,
1) collecting coins using
all vertical lines
(total r - l)
2) collecting coins using
lower horizontal lines
and recursively on left
and right segments */
return min($r - $l,
minStepsRecur($height, $l, $m, $height[$m]) +
minStepsRecur($height, $m + 1, $r, $height[$m]) +
$height[$m] - $h);
}
// method returns minimum number of step to
// collect coin from stack, with height in
// height[] array
function minSteps($height, $N)
{
return minStepsRecur($height, 0, $N, 0);
}
// Driver Code
$height = array(2, 1, 2, 5, 1);
$N = sizeof($height);
echo minSteps($height, $N) ;
// This code is contributed by nitin mittal.
?>
JavaScript
<script>
// Javascript Code to Collect all coins in
// minimum number of steps
// recursive method to collect coins from
// height array l to r, with height h already
// collected
function minStepsRecur(height,l,r,h)
{
// if l is more than r, no steps needed
if (l >= r)
return 0;
// loop over heights to get minimum height
// index
let m = l;
for (let i = l; i < r; i++)
if (height[i] < height[m])
m = i;
/* choose minimum from,
1) collecting coins using all vertical
lines (total r - l)
2) collecting coins using lower horizontal
lines and recursively on left and right
segments */
return Math.min(r - l,
minStepsRecur(height, l, m, height[m]) +
minStepsRecur(height, m + 1, r, height[m]) +
height[m] - h);
}
// method returns minimum number of step to
// collect coin from stack, with height in
// height[] array
function minSteps(height,N)
{
return minStepsRecur(height, 0, N, 0);
}
/* Driver program to test above function */
let height=[2, 1, 2, 5, 1 ];
let N = height.length;
document.write(minSteps(height, N));
// This code is contributed by avanitrachhadiya2155
</script>
Output:
4
Time complexity: The time complexity of this algorithm is O(N^2), where N is the number of elements in the height array.
Space complexity: The space complexity of this algorithm is O(N) due to the recursive calls that are made on the height array.
Similar Reads
Minimum Number of Coins in a Bag Given an array arr[] where each element represents the number of coins in the bag, also given an integer k, minimize the maximum number of coins present in any bag using at most k operations, In each operation the coins in one bag can be distributed into two bags. Examples: Input: arr[] = [9], k = 2
9 min read
Minimum cost to convert a '1' to a given number Given a positive integer 'n'. Starting with 1 find the minimum cost required to convert it to n using the following operations : Multiply the number by 2 with costs aAdd 1 to the number with costs bSubtract 1 from the number with costs c.Examples: Input: n = 31, a = 2, b = 5, c = 3Output: 13Explanat
10 min read
Minimum count of elements that sums to a given number Given infinite number of elements of form 10^n and 25*100^n ( n >= 0 ). The task is to find the minimum count of elements chosen such that there sum is equal to K. Examples: Input : K = 48 Output : 6 elements chosen are: (1 + 1 + 1 + 10 + 10 + 25)Input : 69 Output : 9 elements chosen are: (1 + 1
7 min read
Greedy Algorithm to find Minimum number of Coins Given an amount of n Rupees and an infinite supply of each of the denominations {1, 2, 5, 10} valued coins/notes, The task is to find the minimum number of coins needed to make the given amount. Examples: Input: n= 39Output: 6Explanation: We need a 50 Rs note and a 20 Rs note.Input: n = 121Output: 1
3 min read
Optimal cell in matrix to collect maximum coins Given an M * N matrix matrix[][], where each cell is either an obstacle '#', a coin 'C', or empty '0', the task is to choose one empty cell optimally to maximize the coins collected. You can collect coins from the same row and column where you stand until there are no obstacles. Note: We can only st
14 min read