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: 21
💡 Note: Log n=7: Cut into pieces [3,3,1]. Cost = 3×4 + 3×1 = 15. Log m=4: Cut into pieces [3,1]. Cost = 3×1 = 3. Total = 15 + 6 = 21.
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
Log Transportation Problem: n=7, m=4, k=3Input:Log 1: Length 7Log 2: Length 4Truck capacity: k = 3Cutting Process:34Cut cost: 3×4=1231Cut cost: 3×1=331Cut cost: 3×1=3Result:Total Cost = 12 + 3 + 3 = 18All pieces fit in trucks (≤ 3 units each)
Understanding the Visualization
1
Input
Two logs of lengths n=7, m=4 with truck capacity k=3
2
Process
Cut logs to fit: minimize total cutting cost
3
Output
Minimum cost = 21
Key Takeaway
🎯 Key Insight: Cut pieces of maximum size k from the end to minimize total cost
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