diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README.md b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README.md index 4cf210654fce1..a61f873580752 100644 --- a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README.md +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README.md @@ -64,32 +64,68 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3560.Fi -### 方法一 +### 方法一:数学 + +如果两根木材的长度都不超过卡车的最大载重 $k$,则不需要切割,直接返回 $0$。 + +否则,说明只有一个木材的长度超过了 $k$,我们需要将其切割成两段。设较长的木材长度为 $x$,则切割成本为 $k \times (x - k)$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def minCuttingCost(self, n: int, m: int, k: int) -> int: + x = max(n, m) + return 0 if x <= k else k * (x - k) ``` #### Java ```java - +class Solution { +public: + long long minCuttingCost(int n, int m, int k) { + int x = max(n, m); + return x <= k ? 0 : 1LL * k * (x - k); + } +}; ``` #### C++ ```cpp - +class Solution { +public: + long long minCuttingCost(int n, int m, int k) { + int x = max(n, m); + return x <= k ? 0 : 1LL * k * (x - k); + } +}; ``` #### Go ```go +func minCuttingCost(n int, m int, k int) int64 { + x := max(n, m) + if x <= k { + return 0 + } + return int64(k * (x - k)) +} +``` + +#### TypeScript +```ts +function minCuttingCost(n: number, m: number, k: number): number { + const x = Math.max(n, m); + return x <= k ? 0 : k * (x - k); +} ``` diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README_EN.md b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README_EN.md index 623d28003e788..61e1e379cc4db 100644 --- a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README_EN.md +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/README_EN.md @@ -62,32 +62,67 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3560.Fi -### Solution 1 +### Solution 1: Mathematics + +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)$. #### Python3 ```python - +class Solution: + def minCuttingCost(self, n: int, m: int, k: int) -> int: + x = max(n, m) + return 0 if x <= k else k * (x - k) ``` #### Java ```java - +class Solution { + public long minCuttingCost(int n, int m, int k) { + int x = Math.max(n, m); + return x <= k ? 0 : 1L * k * (x - k); + } +} ``` #### C++ ```cpp - +class Solution { +public: + long long minCuttingCost(int n, int m, int k) { + int x = max(n, m); + return x <= k ? 0 : 1LL * k * (x - k); + } +}; ``` #### Go ```go +func minCuttingCost(n int, m int, k int) int64 { + x := max(n, m) + if x <= k { + return 0 + } + return int64(k * (x - k)) +} +``` + +#### TypeScript +```ts +function minCuttingCost(n: number, m: number, k: number): number { + const x = Math.max(n, m); + return x <= k ? 0 : k * (x - k); +} ``` diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.cpp b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.cpp new file mode 100644 index 0000000000000..d4b81a14b0ac3 --- /dev/null +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.cpp @@ -0,0 +1,7 @@ +class Solution { +public: + long long minCuttingCost(int n, int m, int k) { + int x = max(n, m); + return x <= k ? 0 : 1LL * k * (x - k); + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.go b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.go new file mode 100644 index 0000000000000..2f4bcdf7ce366 --- /dev/null +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.go @@ -0,0 +1,7 @@ +func minCuttingCost(n int, m int, k int) int64 { + x := max(n, m) + if x <= k { + return 0 + } + return int64(k * (x - k)) +} \ No newline at end of file diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.java b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.java new file mode 100644 index 0000000000000..bfbfcc15be6bf --- /dev/null +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.java @@ -0,0 +1,6 @@ +class Solution { + public long minCuttingCost(int n, int m, int k) { + int x = Math.max(n, m); + return x <= k ? 0 : 1L * k * (x - k); + } +} \ No newline at end of file diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.py b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.py new file mode 100644 index 0000000000000..2add5827f7578 --- /dev/null +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.py @@ -0,0 +1,4 @@ +class Solution: + def minCuttingCost(self, n: int, m: int, k: int) -> int: + x = max(n, m) + return 0 if x <= k else k * (x - k) diff --git a/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.ts b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.ts new file mode 100644 index 0000000000000..ada7d0610b8de --- /dev/null +++ b/solution/3500-3599/3560.Find Minimum Log Transportation Cost/Solution.ts @@ -0,0 +1,4 @@ +function minCuttingCost(n: number, m: number, k: number): number { + const x = Math.max(n, m); + return x <= k ? 0 : k * (x - k); +}