Find Minimum Log Transportation Cost - Problem

You are given integers n, m, and k. There are two logs of lengths n and m units, which need to be transported in three trucks where each truck can carry one log with length at most k units.

You may cut the logs into smaller pieces, where the cost of cutting a log of length x into logs of length len1 and len2 is cost = len1 * len2 such that len1 + len2 = x.

Return the minimum total cost to distribute the logs onto the trucks. If the logs don't need to be cut, the total cost is 0.

Input & Output

Example 1 — Basic Case
$ Input: n = 7, m = 4, k = 3
Output: 18
💡 Note: Log n=7: Cut into pieces [3,4]. Cost = 3×4 = 12. Then cut 4 into [3,1]. Cost = 3×1 = 3. Total for log 7: 15. Log m=4: Cut into pieces [3,1]. Cost = 3×1 = 3. Total = 15 + 3 = 18.
Example 2 — No Cuts Needed
$ Input: n = 3, m = 2, k = 5
Output: 0
💡 Note: Both logs fit in trucks without cutting since 3 ≤ 5 and 2 ≤ 5. Total cost = 0.
Example 3 — One Log Needs Cutting
$ Input: n = 8, m = 3, k = 4
Output: 16
💡 Note: Log n=8: Cut into [4,4]. Cost = 4×4 = 16. Log m=3: No cut needed. Total = 16.

Constraints

  • 1 ≤ n, m, k ≤ 1000
  • We have exactly 3 trucks available
  • Each truck can carry at most one log

Visualization

Tap to expand
Find Minimum Log Transportation Cost INPUT Log 1 (n = 7 units) 7 units Log 2 (m = 4 units) 4 units 3 Trucks (max k = 3 each) max 3 max 3 max 3 n = 7 m = 4 k = 3 Total: 7+4=11, Need: 3*3=9 Must cut logs! ALGORITHM STEPS 1 Cut Log 1 (7 units) 7 --> 4 + 3 (cost: 4*3=12) 2 Cut the 4-unit piece 4 --> 2 + 2 (cost: 2*2=4) 3 Cut Log 2 (4 units) 4 --> 2 + 2 (cost: 2*2=4) 4 Alternative Optimal Cut 7 --> 3+4, 4 --> 1+3 Cost: 12 + 4 + 3 = 19 Optimal Cutting Strategy 7 --> 3 + 4 (cost: 12) 4 --> 1 + 3 (cost: 3) 4 --> 2 + 2 (cost: 4) 2 --> 1 + 1 (cost: 1) Total: 12 + 3 + 4 + 1 + 1 = 21 FINAL RESULT Final Log Pieces: 3 3 1 2 2 Truck Assignments: Truck 1 3 Truck 2 3 Truck 3 OUTPUT 21 OK - Minimum Cost! All logs fit in 3 trucks with min cutting cost Key Insight: The cutting cost len1 * len2 is minimized when we make balanced cuts. For a log of length x, cutting into pieces a and (x-a) costs a*(x-a). Total cost depends on the sequence of cuts. Use dynamic programming or greedy approach to find minimum total cost to get all pieces <= k. TutorialsPoint - Find Minimum Log Transportation Cost | Optimal Solution
Asked in
Google 25 Microsoft 20 Amazon 15
23.0K Views
Medium Frequency
~15 min Avg. Time
890 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