VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-rectangle-cutting/

⇱ CSES Solutions - Rectangle Cutting - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Rectangle Cutting

Last Updated : 8 Apr, 2024

Given an A X B rectangle, your task is to cut it into squares. On each move you can select a rectangle and cut it into two rectangles in such a way that all side lengths remain integers. What is the minimum possible number of moves?

Examples:

Input: A = 3, B = 5
Output: 3
Explanation: The three moves required to cut the rectangle into squares are:

  • Cut the 3 X 5 rectangle into a square of size 3 X 3 and a rectangle of size 2 X 3.
  • Cut the 2 X 3 rectangle into a square of size 2 X 2 and a rectangle of size 2 X 1.
  • Cut the 2 X 1 rectangle into two squares of size 1 X 1.

Input: A = 5, B = 10
Output: 1
Explanation: Only 1 move is required to cut the rectangle into squares and that is by cutting the 5 X 10 rectangle into 2 squares of size 5 X 5.

Approach: To solve the problem, follow the below idea:

Maintain a dp[][] array such that dp[i][j] stores the minimum number of cuts required to cut an (i X j) rectangle into squares. For an (i X j) rectangle, there can be two cases:

  • Case 1: If (i == j), then the rectangle is already a square. So, dp[i][j] = 0.
  • Case 2: If (i != j), then we need to make the cut either horizontally or vertically.
    • If we make the cut horizontally, then we can cut at any position 1, 2 ... i-1. If we cut at position k, then we are left with two pieces of sizes k X j and (iāˆ’k) X j. Now, we can look up the number of moves to reduce these to squares in the dp[][] array. We can iterate over all possible values of k and find the cut which requires minimum number of moves, that is dp[i][j] = min(dp[i][j], dp[i][k] + dp[i][j - k] + 1)
    • Similarly, if we make the cut vertically, then we can cut at any position 1, 2 ... j-1. If we cut at position k, then we are left with two pieces of sizes i X k and i X (j - k). Now, we can look up the number of moves to reduce these to squares in the dp[][] array. We can iterate over all possible values of k and find the cut which requires minimum number of moves, that is dp[i][j] = min(dp[i][j], dp[k][j] + dp[i - k][j] + 1)

After considering over all the possible cuts, dp[A][B] stores the final answer.

Step-by-step algorithm:

  • Initialize a dp[][] array with a very large number, say 10^9 such that dp[i][j] stores the minimum number of cuts required to cut an (i X j) rectangle into squares.
  • Iterate over all the possible cuts we can make horizontally and iterate over all the cuts we can make vertically.
  • If the length and breadth of the rectangle is 0 (i == j), then dp[i][j] = 0.
  • Otherwise choose the horizontal or vertical cut which requires minimum total moves to reduce the rectangle to squares.
  • Return the final answer as dp[A][B].

Below is the implementation of the algorithm:


Output
3

Time Complexity: O(A * A * B + A * B * B), where A and B are the dimensions of the input rectangle.
Auxiliary Space: O(A * B)

Comment
Article Tags:
Article Tags: