3560. Find Minimum Log Transportation Cost LeetCode Solution

In this guide, you will get 3560. Find Minimum Log Transportation Cost LeetCode Solution with the best time and space complexity. The solution to Find Minimum Log Transportation Cost problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find Minimum Log Transportation Cost solution in C++
  4. Find Minimum Log Transportation Cost solution in Java
  5. Find Minimum Log Transportation Cost solution in Python
  6. Additional Resources
3560. Find Minimum Log Transportation Cost LeetCode Solution image

Problem Statement of Find Minimum Log Transportation Cost

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 <= 105
1 <= n, m <= 2 * k
The input is generated such that it is always possible to transport the logs.

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

3560. Find Minimum Log Transportation Cost LeetCode Solution in C++

class Solution {
 public:
  long long minCuttingCost(long n, long m, int k) {
    return max(0L, max(n, m) - k) * k;
  }
};
/* code provided by PROGIEZ */

3560. Find Minimum Log Transportation Cost LeetCode Solution in Java

class Solution {
  public long minCuttingCost(long n, long m, int k) {
    return Math.max(0, Math.max(n, m) - k) * k;
  }
}
// code provided by PROGIEZ

3560. Find Minimum Log Transportation Cost LeetCode Solution in Python

class Solution:
  def minCuttingCost(self, n: int, m: int, k: int) -> int:
    return max(0, max(n, m) - k) * k
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.