Collect all coins in minimum number of steps – GeeksforGeeks

Given many stacks of coins which are arranged adjacently. We need to collect all these coins in the minimal count of steps where in one mistreat we can collect one horizontal tune of coins or vertical trace of coins and gather 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 separate and appropriate method acting. We can see that it is constantly beneficial to remove horizontal lines from below. Suppose we are working on stacks from liter index to r index in a recursion step, each meter we will choose minimum stature, remove those many horizontal lines after which smokestack will be broken into two parts, liter to minimum and minimum +1 till radius and we will call recursively in those subarrays. Another thing is we can besides collect coins using vertical lines so we will choose minimal between the result of recursive calls and ( r – fifty ) because using ( gas constant – fifty ) upright lines we can always collect all coins.
As each time we are calling each subarray and finding minimum of that, full time complexity of the solution will be O ( N2 )

C++




#include using namespace std; int minStepsRecur( int height[], int l, int r, int h) {           if (l >= r)          return 0;                int m = l;      for ( int i = l; i < r; i++)          if (height[i] < height[m])              m = i;                                                        return min(r - l,                 minStepsRecur(height, l, m, height[m]) +                 minStepsRecur(height, m + 1, r, height[m]) +                 height[m] - h); } int minSteps( int height[], int N) {      return minStepsRecur(height, 0, N, 0); } int main() {      int height[] = { 2, 1, 2, 5, 1 };      int N = sizeof (height) / sizeof ( int );      cout << minSteps(height, N) << endl;      return 0; }



Java




import java.util.*; class GFG {                     public static int minStepsRecur( int height[], int l,                                             int r, int h)      {                   if (l >= r)              return 0 ;                            int m = l;          for ( int i = l; i < r; i++)              if (height[i] < height[m])                  m = i;                                                                                    return Math.min(r - l,                          minStepsRecur(height, l, m, height[m]) +                          minStepsRecur(height, m + 1 , r, height[m]) +                          height[m] - h);      }                     public static int minSteps( int height[], int N)      {          return minStepsRecur(height, 0 , N, 0 );      }           public static void main(String[] args)      {          int height[] = { 2 , 1 , 2 , 5 , 1 };          int N = height.length;          System.out.println(minSteps(height, N));      } }



Python 3




def minStepsRecur(height, l, r, h):                if l > = r:          return 0 ;                m = l      for i in range (l, r):          if height[i] < height[m]:              m = i                                         return min (r - l,              minStepsRecur(height, l, m, height[m]) +

             minStepsRecur(height, m + 1 , r, height[m]) +              height[m] - h) def minSteps(height, N):      return minStepsRecur(height, 0 , N, 0 ) height = [ 2 , 1 , 2 , 5 , 1 ] N = len (height) print (minSteps(height, N))



C#




using System; class GFG {                     public static int minStepsRecur( int [] height, int l,                                             int r, int h)      {                   if (l >= r)              return 0;                            int m = l;          for ( int i = l; i < r; i++)              if (height[i] < height[m])                  m = i;                                                                                    return Math.Min(r - l,                          minStepsRecur(height, l, m, height[m]) +                          minStepsRecur(height, m + 1, r, height[m]) +                          height[m] - h);      }                     public static int minSteps( int [] height, int N)      {          return minStepsRecur(height, 0, N, 0);      }           public static void Main()      {          int [] height = { 2, 1, 2, 5, 1 };          int N = height.Length;          Console.Write(minSteps(height, N));      } }



PHP




function minStepsRecur( $height , $l ,                              $r , $h ) {                     if ( $l >= $r )          return 0;                     $m = $l ;      for ( $i = $l ; $i < $r ; $i ++)          if ( $height [ $i ] < $height [ $m ])              $m = $i ;                                                                                         return min( $r - $l ,             minStepsRecur( $height , $l , $m , $height [ $m ]) +             minStepsRecur( $height , $m + 1, $r , $height [ $m ]) +             $height [ $m ] - $h ); } function minSteps( $height , $N ) {      return minStepsRecur( $height , 0, $N , 0); }           $height = array (2, 1, 2, 5, 1);      $N = sizeof( $height );      echo minSteps( $height , $N ) ;      ?>



Javascript






Output:

4

This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can besides write an article using contribute.geeksforgeeks.org or mail your article to contribute @ geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything faulty, or you want to partake more data about the subject discussed above.

My Personal Notes

arrow_drop_up

generator : https://coinselected.com
Category : Coin collecting

Leave a Reply

Your email address will not be published.