Skip to content

feat: add solutions to lc problem: No.3560 #4443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,32 +64,68 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3560.Fi

<!-- solution:start -->

### 方法一
### 方法一:数学

如果两根木材的长度都不超过卡车的最大载重 $k$,则不需要切割,直接返回 $0$。

否则,说明只有一个木材的长度超过了 $k$,我们需要将其切割成两段。设较长的木材长度为 $x$,则切割成本为 $k \times (x - k)$。

时间复杂度 $O(1)$,空间复杂度 $O(1)$。

<!-- tabs:start -->

#### 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);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,67 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3560.Fi

<!-- solution:start -->

### 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)$.

<!-- tabs:start -->

#### 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);
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
};
Original file line number Diff line number Diff line change
@@ -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))
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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);
}