![]() |
VOOZH | about |
Given a board of dimensions n Ć m that needs to be cut into n Ć m squares. The cost of making a cut along a horizontal or vertical edge is provided in two arrays:
Find the minimum total cost required to cut the board into squares optimally.
Examples:
Input: x[] = [2, 1, 3, 1, 4], y[] = [4, 1, 2], n = 4, m = 6
Output: 42
Explanation:
š ImageInitially no. of horizontal segments = 1 & no. of vertical segments = 1.
Optimal way to cut into square is:
Pick 4 (from x) -> vertical cut, Cost = 4 Ć horizontal segments = 4,
Now, horizontal segments = 1, vertical segments = 2.
Pick 4 (from y) -> horizontal cut, Cost = 4 Ć vertical segments = 8,
Now, horizontal segments = 2, vertical segments = 2.
Pick 3 (from x) -> vertical cut, Cost = 3 Ć horizontal segments = 6,
Now, horizontal segments = 2, vertical segments = 3.
Pick 2 (from x) -> vertical cut, Cost = 2 Ć horizontal segments = 4,
Now, horizontal segments = 2, vertical segments = 4.
Pick 2 (from y) -> horizontal cut, Cost = 2 Ć vertical segments = 8,
Now, horizontal segments = 3, vertical segments = 4.
Pick 1 (from x) -> vertical cut, Cost = 1 Ć horizontal segments = 3,
Now, horizontal segments = 3, vertical segments = 5.
Pick 1 (from x) -> vertical cut, Cost = 1 Ć horizontal segments = 3,
Now, horizontal segments = 3, vertical segments = 6.
Pick 1 (from y) -> horizontal cut, Cost = 1 Ć vertical segments = 6,
Now, horizontal segments = 4, vertical segments = 6.
So, the total cost = 4 + 8 + 6 + 4 + 8 + 3 + 3 + 6 = 42.Input: x[] = [1, 1, 1], y[] = [1, 1, 1], n = 4, m = 4
Output: 15
Explanation:
Initially no. of horizontal segments = 1 & no. of vertical segments = 1.
Optimal way to cut into square is:
Pick 1 (from y) -> horizontal cut, Cost = 1 Ć vertical segments = 1,
Now, horizontal segments = 2, vertical segments = 1.
Pick 1 (from y) -> horizontal cut, Cost = 1 Ć vertical segments = 1,
Now, horizontal segments = 3, vertical segments = 1.
Pick 1 (from y) -> horizontal cut, Cost = 1 Ć vertical segments = 1,
Now, horizontal segments = 4, vertical segments = 1.
Pick 1 (from x) -> vertical cut, Cost = 1 Ć horizontal segments = 4,
Now, horizontal segments = 4, vertical segments = 2.
Pick 1 (from x) -> vertical cut, Cost = 1 Ć horizontal segments = 4,
Now, horizontal segments = 4, vertical segments = 3.
Pick 1 (from x) -> vertical cut, Cost = 1 Ć horizontal segments = 4,
Now, horizontal segments = 4, vertical segments = 4
So, the total cost = 1 + 1 + 1 + 4 + 4 + 4 = 15.
Table of Content
The idea is to generate all possible permutations of the given cuts and then calculate the cost for each permutation. Finally, return the minimum cost among them.
Note: This approach is not feasible for larger inputs because the number of permutations grows factorially as (m+n-2)!.
For each permutation, we must calculate the cost in O(m+n) time. Hence, the overall time complexity becomes O((m+nā2)!Ć(m+n)).
The idea is to make the most expensive cuts first using a greedy approach. The observation is that choosing the highest cost cut at each step reduces future costs by affecting multiple pieces at once. We sort the vertical (x) and horizontal (y) cut costs in descending order, then iteratively pick the larger one to maximize cost savings. The remaining cuts are processed separately to ensure all sections are split optimally.
What happens when we make a cut?
Steps to solve the problem:
42