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 email@example.com. 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.
- Minimum number of squares whose sum equals to given number n
- Minimum number of cubes whose sum equals to given number N
- Minimum number of increasing subsequences
- Allocate minimum number of pages
- Minimum number of jumps to reach end
- Minimum number of times A has to be repeated such that B is a substring of it
- Minimum number of Fibonacci jumps to reach end
- Find minimum number of coins that make a given value
- Minimum number of palindromes required to express N as a sum | Set 2
- Minimum number of palindromes required to express N as a sum | Set 1
- Minimum number of sub-strings of a string such that all are power of 5
- Minimum count of elements that sums to a given number
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Minimum number of deletions to make a string palindrome
- Minimum number of deletions to make a sorted sequence