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 1. 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)
Below is the implementation of above idea using Dynamic Programming.
This article is contributed by Ayush Govil and Aditya Nihal Kumar Singh. 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.
- Non-decreasing subsequence of size k with minimum sum
- Count of Palindromic substrings in an Index range
- Find maximum possible stolen value from houses
- Number of n-digits non-decreasing integers
- Painting Fence Algorithm
- Perfect Sum Problem (Print all subsets with given sum)
- Count of strings that can be formed using a, b and c under given constraints
- Minimum and Maximum values of an expression with * and +
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Word Break Problem | DP-32
- Optimal Strategy for a Game | DP-31
- Minimum insertions to form a palindrome | DP-28
- Longest Common Subsequence | DP-4
- Program for Fibonacci numbers
- Largest Sum Contiguous Subarray