Minimum Cost for Cutting Cake I - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: m = 3, n = 2, horizontalCut = [1,1], verticalCut = [1]
Output: 4
💡 Note: Make any cut first, but optimal order gives total cost 4. For instance: make both horizontal cuts first (cost 1×1 + 1×1 = 2), then vertical cut affects both pieces (cost 1×2 = 2), total = 4.
Example 2 — Different Costs
$ Input: m = 4, n = 3, horizontalCut = [1,2,1], verticalCut = [1,3]
Output: 8
💡 Note: Sort cuts: [3,2,1,1,1]. Make V-cut cost 3 first (3×1=3), H-cut cost 2 (2×2=4), then remaining cuts cost 1 each.
Example 3 — Equal Costs
$ Input: m = 2, n = 2, horizontalCut = [7], verticalCut = [4]
Output: 15
💡 Note: Two cuts needed: Make H-cut first since 7 > 4 (cost 7×1=7), then V-cut affects 2 pieces (cost 4×2=8), total = 15.

Constraints

  • 1 ≤ m, n ≤ 20
  • horizontalCut.length == m - 1
  • verticalCut.length == n - 1
  • 1 ≤ horizontalCut[i], verticalCut[i] ≤ 1000

Visualization

Tap to expand
Minimum Cost for Cutting Cake I Greedy by Cost Priority Approach INPUT 3 x 2 Cake h1=1 h2=1 v1=1 m = 3, n = 2 horizontalCut = [1, 1] verticalCut = [1] Need: (m-1) + (n-1) cuts = 2 + 1 = 3 total cuts Goal: 1x1 pieces Target: 6 pieces total ALGORITHM STEPS 1 Collect All Cuts Merge h-cuts and v-cuts 2 Sort by Cost (DESC) Higher cost first! 3 Process Each Cut Track piece counts 4 Calculate Cost cost x pieces affected Execution Trace: hPieces=1, vPieces=1 Cut h1: 1 x 1 = 1 hPieces = 2 Cut h2: 1 x 1 = 1 hPieces = 3 Cut v1: 1 x 3 = 3 vPieces = 2 FINAL RESULT 1x1 1x1 1x1 1x1 1x1 1x1 6 pieces (3 x 2) Total Cost 4 Cost Breakdown: h1: 1 + h2: 1 + v1: 3 = 1 + 1 + 3 = 4 Key Insight: Make expensive cuts EARLY when fewer pieces exist. Each cut multiplies by number of pieces it crosses. Horizontal cut cost = horizontalCut[i] x (number of vertical pieces already created) Vertical cut cost = verticalCut[j] x (number of horizontal pieces already created) TutorialsPoint - Minimum Cost for Cutting Cake I | Greedy by Cost Priority Approach
Asked in
Google 15 Amazon 12 Microsoft 8
12.5K Views
Medium Frequency
~15 min Avg. Time
420 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