Skip to content

[pull] main from doocs:main #438

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 2 commits into from
Mar 23, 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
33 changes: 29 additions & 4 deletions solution/3400-3499/3492.Maximum Containers on a Ship/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,32 +62,57 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3492.Ma

<!-- solution:start -->

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

我们先计算出船上可以装载的最大重量,即 $n \times n \times w$,然后取其与 $\text{maxWeight}$ 的最小值,再除以 $w$ 即可。

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

<!-- tabs:start -->

#### Python3

```python

class Solution:
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
return min(n * n * w, maxWeight) // w
```

#### Java

```java

class Solution {
public int maxContainers(int n, int w, int maxWeight) {
return Math.min(n * n * w, maxWeight) / w;
}
}
```

#### C++

```cpp

class Solution {
public:
int maxContainers(int n, int w, int maxWeight) {
return min(n * n * w, maxWeight) / w;
}
};
```

#### Go

```go
func maxContainers(n int, w int, maxWeight int) int {
return min(n*n*w, maxWeight) / w
}
```

#### TypeScript

```ts
function maxContainers(n: number, w: number, maxWeight: number): number {
return (Math.min(n * n * w, maxWeight) / w) | 0;
}
```

<!-- tabs:end -->
Expand Down
33 changes: 29 additions & 4 deletions solution/3400-3499/3492.Maximum Containers on a Ship/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,57 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3400-3499/3492.Ma

<!-- solution:start -->

### Solution 1
### Solution 1: Mathematics

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

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
return min(n * n * w, maxWeight) // w
```

#### Java

```java

class Solution {
public int maxContainers(int n, int w, int maxWeight) {
return Math.min(n * n * w, maxWeight) / w;
}
}
```

#### C++

```cpp

class Solution {
public:
int maxContainers(int n, int w, int maxWeight) {
return min(n * n * w, maxWeight) / w;
}
};
```

#### Go

```go
func maxContainers(n int, w int, maxWeight int) int {
return min(n*n*w, maxWeight) / w
}
```

#### TypeScript

```ts
function maxContainers(n: number, w: number, maxWeight: number): number {
return (Math.min(n * n * w, maxWeight) / w) | 0;
}
```

<!-- tabs:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution {
public:
int maxContainers(int n, int w, int maxWeight) {
return min(n * n * w, maxWeight) / w;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
func maxContainers(n int, w int, maxWeight int) int {
return min(n*n*w, maxWeight) / w
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution {
public int maxContainers(int n, int w, int maxWeight) {
return Math.min(n * n * w, maxWeight) / w;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Solution:
def maxContainers(self, n: int, w: int, maxWeight: int) -> int:
return min(n * n * w, maxWeight) // w
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function maxContainers(n: number, w: number, maxWeight: number): number {
return (Math.min(n * n * w, maxWeight) / w) | 0;
}
Loading