Skip to content

feat: add solutions to lc problem: No.3638 #4619

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
Aug 3, 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
75 changes: 71 additions & 4 deletions solution/3600-3699/3638.Maximum Balanced Shipments/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,32 +87,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Ma

<!-- solution:start -->

### 方法一
### 方法一:贪心

我们维护当前遍历的数组的最大值 $\text{mx}$,并遍历数组中的每个元素 $x$。如果 $x < \text{mx}$,则说明当前元素可以作为一个平衡装运的最后一个包裹,因此我们就将答案加一,并将 $\text{mx}$ 重置为 0。否则,我们更新 $\text{mx}$ 为当前元素 $x$ 的值。

遍历结束后,返回答案即可。

时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$,只使用了常数级别的额外空间。

<!-- tabs:start -->

#### Python3

```python

class Solution:
def maxBalancedShipments(self, weight: List[int]) -> int:
ans = mx = 0
for x in weight:
mx = max(mx, x)
if x < mx:
ans += 1
mx = 0
return ans
```

#### Java

```java

class Solution {
public int maxBalancedShipments(int[] weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = Math.max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int maxBalancedShipments(vector<int>& weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
};
```

#### Go

```go
func maxBalancedShipments(weight []int) (ans int) {
mx := 0
for _, x := range weight {
mx = max(mx, x)
if x < mx {
ans++
mx = 0
}
}
return
}
```

#### TypeScript

```ts
function maxBalancedShipments(weight: number[]): number {
let [ans, mx] = [0, 0];
for (const x of weight) {
mx = Math.max(mx, x);
if (x < mx) {
ans++;
mx = 0;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
75 changes: 71 additions & 4 deletions solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Ma

<!-- solution:start -->

### Solution 1
### Solution 1: Greedy

We maintain the maximum value $\text{mx}$ of the currently traversed array, and iterate through each element $x$ in the array. If $x < \text{mx}$, it means the current element can serve as the last parcel of a balanced shipment, so we increment the answer by one and reset $\text{mx}$ to 0. Otherwise, we update $\text{mx}$ to the value of the current element $x$.

After the traversal, we return the answer.

The time complexity is $O(n)$, where $n$ is the length of the array. The space complexity is $O(1)$, using only constant extra space.

<!-- tabs:start -->

#### Python3

```python

class Solution:
def maxBalancedShipments(self, weight: List[int]) -> int:
ans = mx = 0
for x in weight:
mx = max(mx, x)
if x < mx:
ans += 1
mx = 0
return ans
```

#### Java

```java

class Solution {
public int maxBalancedShipments(int[] weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = Math.max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
}
```

#### C++

```cpp

class Solution {
public:
int maxBalancedShipments(vector<int>& weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
};
```

#### Go

```go
func maxBalancedShipments(weight []int) (ans int) {
mx := 0
for _, x := range weight {
mx = max(mx, x)
if x < mx {
ans++
mx = 0
}
}
return
}
```

#### TypeScript

```ts
function maxBalancedShipments(weight: number[]): number {
let [ans, mx] = [0, 0];
for (const x of weight) {
mx = Math.max(mx, x);
if (x < mx) {
ans++;
mx = 0;
}
}
return ans;
}
```

<!-- tabs:end -->
Expand Down
15 changes: 15 additions & 0 deletions solution/3600-3699/3638.Maximum Balanced Shipments/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution {
public:
int maxBalancedShipments(vector<int>& weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
};
11 changes: 11 additions & 0 deletions solution/3600-3699/3638.Maximum Balanced Shipments/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
func maxBalancedShipments(weight []int) (ans int) {
mx := 0
for _, x := range weight {
mx = max(mx, x)
if x < mx {
ans++
mx = 0
}
}
return
}
14 changes: 14 additions & 0 deletions solution/3600-3699/3638.Maximum Balanced Shipments/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int maxBalancedShipments(int[] weight) {
int ans = 0;
int mx = 0;
for (int x : weight) {
mx = Math.max(mx, x);
if (x < mx) {
++ans;
mx = 0;
}
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def maxBalancedShipments(self, weight: List[int]) -> int:
ans = mx = 0
for x in weight:
mx = max(mx, x)
if x < mx:
ans += 1
mx = 0
return ans
11 changes: 11 additions & 0 deletions solution/3600-3699/3638.Maximum Balanced Shipments/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function maxBalancedShipments(weight: number[]): number {
let [ans, mx] = [0, 0];
for (const x of weight) {
mx = Math.max(mx, x);
if (x < mx) {
ans++;
mx = 0;
}
}
return ans;
}