Given a paper of size A x B. Task is to cut the paper into squares of any size. Find the minimum number of squares that can be cut from the paper.
Input : 36 x 30 Output : 5 Explanation : 3 (squares of size 12x12) + 2 (squares of size 18x18) Input : 4 x 5 Output : 5 Explanation : 1 (squares of size 4x4) + 4 (squares of size 1x1)
Asked in : Google
We have already discussed the Greedy approach to solve this problem in previous article. But the previous approach doesn’t always work. For example, it fails for the above first test case. So, in this article we solve this problem using Dynamic Programming.
We know that if we want to cut minimum number of squares from the paper then we would have to cut largest square possible from the paper first and largest square will have same side as smaller side of the paper. For example if paper have the size 13 x 29, then maximum square will be of side 13. so we can cut 2 square of size 13 x 13 (29/13 = 2). Now remaining paper will have size 3 x 13. Similarly we can cut remaining paper by using 4 squares of size 3 x 3 and 3 squares of 1 x 1. So minimum 9 squares can be cut from the Paper of size 13 x 29.
minimumSquare is a function which tries to split the rectangle at some position. The function is called recursively for both parts. Try all possible splits and take the one with minimum result. The base case is when both sides are equal i.e the input is already a square, in which case the result is We are trying to find the cut which is nearest to the center which will lead us to our minimum result.
Assuming we have a rectangle with width is N and height is M.
- if (N == M), so it is a square and nothing need to be done.
- Otherwise, we can divide the rectangle into two other smaller one (N – x, M) and (x, M), so it can be solved recursively.
- Similarly, we can also divide it into (N, M – x) and (N, x)
Also we need to be aware of an edge case here which is N=11 and M=13 or vice versa. The following will be the best possible answer for the given test case:
Our Approach will return 8 for this case but as you can see we can cut the paper in 6 squares which is minimum. This happens because there is no vertical or horizontal line that cuts through the whole square in the optimum solution.
Below is the implementation of the above idea using Dynamic Programming.
This article is contributed by Ayush Govil, Aditya Nihal Kumar Singh and Deepanshu Aggarwal. 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.
- Number of ways to cut a stick of length N into in even length at most K units long pieces
- Minimum number of squares whose sum equals to given number N | set 2
- Maximise number of cuts in a rod if it can be cut only in given 3 sizes
- Minimum number of squares whose sum equals to given number n
- Partition a set into two subsets such that the difference of subset sums is minimum
- Minimum number of deletions and insertions to transform one string into another
- Minimum splits required to convert a number into prime segments
- Split array into minimum number of subsets having maximum pair sum at most K
- Count number of ways to partition a set into k subsets
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split the string into minimum parts such that each part is in the another string
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Count possible splits of sum N into K integers such that the minimum is at least P
- Partition of a set into K subsets with equal sum using BitMask and DP
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Number of ways to partition a string into two balanced subsequences
- Number of ways to divide an array into K equal sum sub-arrays
- Count of ways to split a given number into prime segments
- Split array into two equal length subsets such that all repetitions of a number lies in a single subset
- Minimum number of jumps to reach end | Set 2 (O(n) solution)