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 4b39d60

Browse files
feat: add solutions to lc problem: No.3492 (#4289)
No.3492.Maximum Containers on a Ship
1 parent a81893f commit 4b39d60

File tree

7 files changed

+78
-8
lines changed

7 files changed

+78
-8
lines changed

‎solution/3400-3499/3492.Maximum Containers on a Ship/README.md‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,32 +62,57 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3492.Ma
6262

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

65-
### 方法一
65+
### 方法一:数学
66+
67+
我们先计算出船上可以装载的最大重量,即 $n \times n \times w,ドル然后取其与 $\text{maxWeight}$ 的最小值,再除以 $w$ 即可。
68+
69+
时间复杂度 $O(1),ドル空间复杂度 $O(1)$。
6670

6771
<!-- tabs:start -->
6872

6973
#### Python3
7074

7175
```python
72-
76+
class Solution:
77+
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
78+
return min(n * n * w, maxWeight) // w
7379
```
7480

7581
#### Java
7682

7783
```java
78-
84+
class Solution {
85+
public int maxContainers(int n, int w, int maxWeight) {
86+
return Math.min(n * n * w, maxWeight) / w;
87+
}
88+
}
7989
```
8090

8191
#### C++
8292

8393
```cpp
84-
94+
class Solution {
95+
public:
96+
int maxContainers(int n, int w, int maxWeight) {
97+
return min(n * n * w, maxWeight) / w;
98+
}
99+
};
85100
```
86101
87102
#### Go
88103
89104
```go
105+
func maxContainers(n int, w int, maxWeight int) int {
106+
return min(n*n*w, maxWeight) / w
107+
}
108+
```
109+
110+
#### TypeScript
90111

112+
```ts
113+
function maxContainers(n: number, w: number, maxWeight: number): number {
114+
return (Math.min(n * n * w, maxWeight) / w) | 0;
115+
}
91116
```
92117

93118
<!-- tabs:end -->

‎solution/3400-3499/3492.Maximum Containers on a Ship/README_EN.md‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,57 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3492.Ma
6060

6161
<!-- solution:start -->
6262

63-
### Solution 1
63+
### Solution 1: Mathematics
64+
65+
First, we calculate the maximum weight the boat can carry, which is $n \times n \times w$. Then, we take the minimum of this value and $\text{maxWeight},ドル and divide it by $w$.
66+
67+
The time complexity is $O(1),ドル and the space complexity is $O(1)$.
6468

6569
<!-- tabs:start -->
6670

6771
#### Python3
6872

6973
```python
70-
74+
class Solution:
75+
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
76+
return min(n * n * w, maxWeight) // w
7177
```
7278

7379
#### Java
7480

7581
```java
76-
82+
class Solution {
83+
public int maxContainers(int n, int w, int maxWeight) {
84+
return Math.min(n * n * w, maxWeight) / w;
85+
}
86+
}
7787
```
7888

7989
#### C++
8090

8191
```cpp
82-
92+
class Solution {
93+
public:
94+
int maxContainers(int n, int w, int maxWeight) {
95+
return min(n * n * w, maxWeight) / w;
96+
}
97+
};
8398
```
8499
85100
#### Go
86101
87102
```go
103+
func maxContainers(n int, w int, maxWeight int) int {
104+
return min(n*n*w, maxWeight) / w
105+
}
106+
```
107+
108+
#### TypeScript
88109

110+
```ts
111+
function maxContainers(n: number, w: number, maxWeight: number): number {
112+
return (Math.min(n * n * w, maxWeight) / w) | 0;
113+
}
89114
```
90115

91116
<!-- tabs:end -->
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution {
2+
public:
3+
int maxContainers(int n, int w, int maxWeight) {
4+
return min(n * n * w, maxWeight) / w;
5+
}
6+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func maxContainers(n int, w int, maxWeight int) int {
2+
return min(n*n*w, maxWeight) / w
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public int maxContainers(int n, int w, int maxWeight) {
3+
return Math.min(n * n * w, maxWeight) / w;
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class Solution:
2+
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
3+
return min(n * n * w, maxWeight) // w
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function maxContainers(n: number, w: number, maxWeight: number): number {
2+
return (Math.min(n * n * w, maxWeight) / w) | 0;
3+
}

0 commit comments

Comments
(0)

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