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.
Example 1:
Input: n = 6, m = 5, k = 5
Output: 5
Explanation:
Cut the log with length 6 into logs with length 1 and 5, at a cost equal to 1 * 5 == 5. Now the three logs of length 1, 5, and 5 can fit in one truck each.
Example 2:
Input: n = 4, m = 4, k = 6
Output: 0
Explanation:
The two logs can fit in the trucks already, hence we don't need to cut the logs.
Constraints:
2 <= k <= 1051 <= n, m <= 2 * kProblem Overview: You are given constraints around transporting logs and need to compute the minimum total transportation cost. The key task is determining how to group logs efficiently so the total cost of all transport operations is minimized.
Approach 1: Direct Simulation (O(n) time, O(1) space)
A straightforward idea is to simulate the transportation process step by step. You repeatedly move a batch of logs, update the remaining count, and accumulate the cost for each trip. This works because you explicitly model each transportation operation until all logs are delivered. The drawback is unnecessary iteration when the number of logs is large, since the same calculation can be derived mathematically instead of simulating each step.
Approach 2: Mathematics / Closed-Form Formula (O(1) time, O(1) space)
The optimal solution comes from observing that the number of transport operations follows a predictable pattern. Instead of simulating trips, compute how many batches are required using ceiling division. If each operation can move a fixed number of logs, the number of trips becomes ceil(totalLogs / capacity). The final transportation cost is then calculated directly using this value and the cost rule defined in the problem.
This approach relies purely on arithmetic operations. No iteration over logs is required. The main insight is recognizing that batching determines the number of transport operations, and that batching can be calculated with integer math. Problems like this frequently appear in math-focused interview questions where deriving the correct formula eliminates simulation.
In practice, you compute the number of trips with (n + k - 1) / k style integer math and multiply it by the cost per operation. This technique is common in problems involving batching, grouping, or resource allocation and appears across many greedy and mathematical optimizations.
Recommended for interviews: Start by explaining the simulation idea to demonstrate understanding of the process. Then derive the mathematical shortcut using ceiling division. Interviewers expect the constant-time formula because it shows you can recognize patterns and remove unnecessary iteration.
If the lengths of both logs do not exceed the truck's maximum load k, then no cutting is needed, and we simply return 0.
Otherwise, it means that only one log has a length greater than k, and we need to cut it into two pieces. Let the longer log have length x, then the cutting cost is k times (x - k).
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Simulation | O(n) | O(1) | Useful for understanding the transportation process step by step |
| Mathematical Formula (Ceiling Division) | O(1) | O(1) | Best choice when trips can be computed directly using arithmetic batching |
Find Minimum Log Transportation Cost | Weekly Contest 451 | Java | Developer Coder • Developer Coder • 688 views views
Watch 7 more video solutions →Practice Find Minimum Log Transportation Cost with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor