#3560
Find Minimum Log Transportation Cost
EasyMathGreedyDynamic Programming
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(1) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(1)Space O(1)
Directly assess if logs need cutting based on their lengths compared to k. If both logs exceed k, calculate the minimal cut needed to fit them into trucks.
⚙️
Algorithm
3 steps- 1Step 1: If both n and m are less than or equal to k, return 0.
- 2Step 2: If both n and m are greater than k, calculate the minimal cut cost as (n - k) * (m - k).
- 3Step 3: If only one log exceeds k, cut it to fit and return the cost.
solution.py6 lines
1def min_cost_optimal(n, m, k):
2 if n <= k and m <= k:
3 return 0
4 if n > k and m > k:
5 return (n - k) * (m - k)
6 return 0ℹ
Complexity note: Constant time complexity since we are only performing a few comparisons and calculations regardless of input size.
- 1Logs fitting without cuts saves cost.
- 2Minimize cuts by assessing lengths directly.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.