3218. Minimum Cost for Cutting Cake I LeetCode Solution
In this guide, you will get 3218. Minimum Cost for Cutting Cake I LeetCode Solution with the best time and space complexity. The solution to Minimum Cost for Cutting Cake I problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Minimum Cost for Cutting Cake I solution in C++
- Minimum Cost for Cutting Cake I solution in Java
- Minimum Cost for Cutting Cake I solution in Python
- Additional Resources

Problem Statement of Minimum Cost for Cutting Cake I
There is an m x n cake that needs to be cut into 1 x 1 pieces.
You are given integers m, n, and two arrays:
horizontalCut of size m – 1, where horizontalCut[i] represents the cost to cut along the horizontal line i.
verticalCut of size n – 1, where verticalCut[j] represents the cost to cut along the vertical line j.
In one operation, you can choose any piece of cake that is not yet a 1 x 1 square and perform one of the following cuts:
Cut along a horizontal line i at a cost of horizontalCut[i].
Cut along a vertical line j at a cost of verticalCut[j].
After the cut, the piece of cake is divided into two distinct pieces.
The cost of a cut depends only on the initial cost of the line and does not change.
Return the minimum total cost to cut the entire cake into 1 x 1 pieces.
Example 1:
Input: m = 3, n = 2, horizontalCut = [1,3], verticalCut = [5]
Output: 13
Explanation:
Perform a cut on the vertical line 0 with cost 5, current total cost is 5.
Perform a cut on the horizontal line 0 on 3 x 1 subgrid with cost 1.
Perform a cut on the horizontal line 0 on 3 x 1 subgrid with cost 1.
Perform a cut on the horizontal line 1 on 2 x 1 subgrid with cost 3.
Perform a cut on the horizontal line 1 on 2 x 1 subgrid with cost 3.
The total cost is 5 + 1 + 1 + 3 + 3 = 13.
Example 2:
Input: m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
Output: 15
Explanation:
Perform a cut on the horizontal line 0 with cost 7.
Perform a cut on the vertical line 0 on 1 x 2 subgrid with cost 4.
Perform a cut on the vertical line 0 on 1 x 2 subgrid with cost 4.
The total cost is 7 + 4 + 4 = 15.
Constraints:
1 <= m, n <= 20
horizontalCut.length == m – 1
verticalCut.length == n – 1
1 <= horizontalCut[i], verticalCut[i] <= 103
Complexity Analysis
- Time Complexity: O(\texttt{sort}(\texttt{horizontalCut}) + \texttt{sort}(\texttt{verticalCut}))
- Space Complexity: O(\texttt{sort}(\texttt{horizontalCut}) + \texttt{sort}(\texttt{verticalCut}))
3218. Minimum Cost for Cutting Cake I LeetCode Solution in C++
class Solution {
public:
int minimumCost(int m, int n, vector<int>& horizontalCut,
vector<int>& verticalCut) {
int cost = 0;
int sumH = accumulate(horizontalCut.begin(), horizontalCut.end(), 0);
int sumV = accumulate(verticalCut.begin(), verticalCut.end(), 0);
ranges::sort(horizontalCut, greater<>());
ranges::sort(verticalCut, greater<>());
for (int i = 0, j = 0; i < m - 1 && j < n - 1;)
if (horizontalCut[i] > verticalCut[j]) {
cost += horizontalCut[i] + sumV;
sumH -= horizontalCut[i++];
} else {
cost += verticalCut[j] + sumH;
sumV -= verticalCut[j++];
}
return cost + sumH + sumV;
}
};
/* code provided by PROGIEZ */
3218. Minimum Cost for Cutting Cake I LeetCode Solution in Java
class Solution {
public int minimumCost(int m, int n, int[] horizontalCut, int[] verticalCut) {
int cost = 0;
int sumH = Arrays.stream(horizontalCut).sum();
int sumV = Arrays.stream(verticalCut).sum();
Arrays.sort(horizontalCut);
Arrays.sort(verticalCut);
for (int i = m - 2, j = n - 2; i >= 0 && j >= 0;)
if (horizontalCut[i] > verticalCut[j]) {
cost += horizontalCut[i] + sumV;
sumH -= horizontalCut[i--];
} else {
cost += verticalCut[j] + sumH;
sumV -= verticalCut[j--];
}
return cost + sumH + sumV;
}
}
// code provided by PROGIEZ
3218. Minimum Cost for Cutting Cake I LeetCode Solution in Python
class Solution:
def minimumCost(
self,
m: int,
n: int,
horizontalCut: list[int],
verticalCut: list[int],
) -> int:
ans = 0
sumH = sum(horizontalCut)
sumV = sum(verticalCut)
horizontalCut.sort()
verticalCut.sort()
while horizontalCut and verticalCut:
if horizontalCut[-1] > verticalCut[-1]:
ans += horizontalCut[-1] + sumV
sumH -= horizontalCut.pop()
else:
ans += verticalCut[-1] + sumH
sumV -= verticalCut.pop()
return ans + sumH + sumV
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.