Minimum Cost for Cutting Cake II - Problem

Imagine you're a professional baker with a beautiful m × n rectangular cake that needs to be cut into individual 1 × 1 pieces for serving. However, each cut comes with a cost!

The Challenge: You have two types of cuts available:

  • Horizontal cuts: Cut along any horizontal line i at cost horizontalCut[i]
  • Vertical cuts: Cut along any vertical line j at cost verticalCut[j]

Key insight: When you make a cut, it affects ALL pieces that cross that line. For example, if you've already made some vertical cuts creating multiple columns, a horizontal cut will cost the same but will cut through ALL those columns simultaneously!

Goal: Find the minimum total cost to cut the entire cake into 1 × 1 pieces.

Input: Dimensions m, n, and arrays horizontalCut (size m-1) and verticalCut (size n-1) representing cut costs.

Output: Minimum total cost as an integer.

Input & Output

example_1.py — Small Cake
$ Input: m = 3, n = 3 horizontalCut = [1, 3] verticalCut = [5]
Output: 13
💡 Note: Sort cuts: [5(V), 3(H), 1(H)]. Cost = 5×1 + 3×2 + 1×2 = 5+6+2 = 13. The expensive vertical cut (5) is made first when it only affects 1 piece, then horizontal cuts affect 2 pieces each.
example_2.py — Larger Example
$ Input: m = 4, n = 4 horizontalCut = [4, 6, 2] verticalCut = [7, 2, 1]
Output: 54
💡 Note: Sorted cuts: [7(V), 6(H), 4(H), 2(V), 2(H), 1(V)]. Processing: 7×1=7, 6×2=12, 4×2=8, 2×3=6, 2×3=6, 1×4=4. Total = 54. Notice how later cuts have higher multipliers.
example_3.py — Edge Case
$ Input: m = 2, n = 2 horizontalCut = [7] verticalCut = [4]
Output: 15
💡 Note: Only two cuts needed. Sort: [7(H), 4(V)]. Cost = 7×1 + 4×2 = 7+8 = 15. The horizontal cut goes first, then vertical cut affects 2 horizontal pieces.

Constraints

  • 1 ≤ m, n ≤ 105
  • horizontalCut.length == m - 1
  • verticalCut.length == n - 1
  • 1 ≤ horizontalCut[i], verticalCut[i] ≤ 106
  • The answer may be large, consider using long long in C++

Visualization

Tap to expand
🎂 Smart Baker's Cake Cutting StrategyThe Problem: Cut 4×3 cake into 1×1 piecesOriginal Cake4 × 3Horizontal Cuts: [4, 6, 2]Vertical Cuts: [7, 2, 1]Goal: Minimize total cutting costKey insight: expensive cuts first!Step 1: Sort cuts by cost (descending)V: 7H: 6H: 4H: 2V: 2V: 1Step 2: Cut greedily - most expensive firstCut V:7Cost = 7×1 = 7✓ V cut: cost 7, affects 1 piecev_cuts = 1, h_cuts = 0Cut H:6Cost = 6×2 = 12✓ H cut: cost 6, affects 2 piecesv_cuts = 1, h_cuts = 1🎯 Final ResultTotal Cost: 7 + 12 + 8 + 4 + 6 + 3 = 40Greedy works because expensive cuts should affect fewer pieces!Time: O((m+n) log(m+n)) | Space: O(m+n)
Understanding the Visualization
1
Identify All Cuts
List all possible horizontal and vertical cuts with their costs
2
Sort by Cost
Arrange cuts from most expensive to least expensive
3
Cut Greedily
Always make the most expensive remaining cut first
4
Calculate Multiplier
Each cut affects all perpendicular pieces created so far
Key Takeaway
🎯 Key Insight: The greedy approach works because making expensive cuts first minimizes their multiplication factor, leading to the optimal total cost.
Asked in
Google 85 Meta 72 Amazon 68 Microsoft 45 Apple 31
43.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen