Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 352832e

Browse files
feat: add solutions to lc problem: No.3560 (doocs#4443)
No.3560.Find Minimum Log Transportation Cost
1 parent 8e777ff commit 352832e

File tree

7 files changed

+107
-8
lines changed

7 files changed

+107
-8
lines changed

‎solution/3500-3599/3560.Find Minimum Log Transportation Cost/README.md‎

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,32 +64,68 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3560.Fi
6464

6565
<!-- solution:start -->
6666

67-
### 方法一
67+
### 方法一:数学
68+
69+
如果两根木材的长度都不超过卡车的最大载重 $k,ドル则不需要切割,直接返回 0ドル$。
70+
71+
否则,说明只有一个木材的长度超过了 $k,ドル我们需要将其切割成两段。设较长的木材长度为 $x,ドル则切割成本为 $k \times (x - k)$。
72+
73+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
6874

6975
<!-- tabs:start -->
7076

7177
#### Python3
7278

7379
```python
74-
80+
class Solution:
81+
def minCuttingCost(self, n: int, m: int, k: int) -> int:
82+
x = max(n, m)
83+
return 0 if x <= k else k * (x - k)
7584
```
7685

7786
#### Java
7887

7988
```java
80-
89+
class Solution {
90+
public:
91+
long long minCuttingCost(int n, int m, int k) {
92+
int x = max(n, m);
93+
return x <= k ? 0 : 1LL * k * (x - k);
94+
}
95+
};
8196
```
8297

8398
#### C++
8499

85100
```cpp
86-
101+
class Solution {
102+
public:
103+
long long minCuttingCost(int n, int m, int k) {
104+
int x = max(n, m);
105+
return x <= k ? 0 : 1LL * k * (x - k);
106+
}
107+
};
87108
```
88109
89110
#### Go
90111
91112
```go
113+
func minCuttingCost(n int, m int, k int) int64 {
114+
x := max(n, m)
115+
if x <= k {
116+
return 0
117+
}
118+
return int64(k * (x - k))
119+
}
120+
```
121+
122+
#### TypeScript
92123

124+
```ts
125+
function minCuttingCost(n: number, m: number, k: number): number {
126+
const x = Math.max(n, m);
127+
return x <= k ? 0 : k * (x - k);
128+
}
93129
```
94130

95131
<!-- tabs:end -->

‎solution/3500-3599/3560.Find Minimum Log Transportation Cost/README_EN.md‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,67 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3560.Fi
6262

6363
<!-- solution:start -->
6464

65-
### Solution 1
65+
### Solution 1: Mathematics
66+
67+
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ドル$.
68+
69+
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)$.
70+
71+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
6672

6773
<!-- tabs:start -->
6874

6975
#### Python3
7076

7177
```python
72-
78+
class Solution:
79+
def minCuttingCost(self, n: int, m: int, k: int) -> int:
80+
x = max(n, m)
81+
return 0 if x <= k else k * (x - k)
7382
```
7483

7584
#### Java
7685

7786
```java
78-
87+
class Solution {
88+
public long minCuttingCost(int n, int m, int k) {
89+
int x = Math.max(n, m);
90+
return x <= k ? 0 : 1L * k * (x - k);
91+
}
92+
}
7993
```
8094

8195
#### C++
8296

8397
```cpp
84-
98+
class Solution {
99+
public:
100+
long long minCuttingCost(int n, int m, int k) {
101+
int x = max(n, m);
102+
return x <= k ? 0 : 1LL * k * (x - k);
103+
}
104+
};
85105
```
86106
87107
#### Go
88108
89109
```go
110+
func minCuttingCost(n int, m int, k int) int64 {
111+
x := max(n, m)
112+
if x <= k {
113+
return 0
114+
}
115+
return int64(k * (x - k))
116+
}
117+
```
118+
119+
#### TypeScript
90120

121+
```ts
122+
function minCuttingCost(n: number, m: number, k: number): number {
123+
const x = Math.max(n, m);
124+
return x <= k ? 0 : k * (x - k);
125+
}
91126
```
92127

93128
<!-- tabs:end -->
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution {
2+
public:
3+
long long minCuttingCost(int n, int m, int k) {
4+
int x = max(n, m);
5+
return x <= k ? 0 : 1LL * k * (x - k);
6+
}
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
func minCuttingCost(n int, m int, k int) int64 {
2+
x := max(n, m)
3+
if x <= k {
4+
return 0
5+
}
6+
return int64(k * (x - k))
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public long minCuttingCost(int n, int m, int k) {
3+
int x = Math.max(n, m);
4+
return x <= k ? 0 : 1L * k * (x - k);
5+
}
6+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def minCuttingCost(self, n: int, m: int, k: int) -> int:
3+
x = max(n, m)
4+
return 0 if x <= k else k * (x - k)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function minCuttingCost(n: number, m: number, k: number): number {
2+
const x = Math.max(n, m);
3+
return x <= k ? 0 : k * (x - k);
4+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /