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.
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)
This article is contributed by Utkarsh Trivedi. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find minimum steps required to reach the end of a matrix | Set - 1
- Minimum steps to delete a string by deleting substring comprising of same characters
- Find minimum steps required to reach the end of a matrix | Set 2
- Largest number N which can be reduced to 0 in K steps
- Maximum and minimum of an array using minimum number of comparisons
- Minimum number N such that total set bits of all numbers from 1 to N is at-least X
- Minimum capacity of small arrays needed to contain all element of the given array
- Allocate minimum number of pages
- Minimum value of K such that sum of cubes of first K natural number is greater than equal to N
- Minimum difference between adjacent elements of array which contain elements from each row of a matrix
- Place k elements such that minimum distance is maximized
- Minimum in an array which is first decreasing then increasing
- Minimum K such that every substring of length atleast K contains a character c
- Minimum operations of the given type required to make a complete graph
- Place the prisoners into cells to maximize the minimum difference between any two
- Minimum K such that sum of array elements after division by K does not exceed S
- Minimum cost of reducing Array by merging any adjacent elements repetitively
- Minimum window size containing atleast P primes in every window of given range
- Find minimum y coordinates from set of N lines in a plane
- Split a given array into K subarrays minimizing the difference between their maximum and minimum