Skip to content

Commit 68291ca

Browse files
authored
feat: add solutions to lc problem: No.3638 (#4619)
No.3638.Maximum Balanced Shipments
1 parent 92eac10 commit 68291ca

File tree

7 files changed

+202
-8
lines changed

7 files changed

+202
-8
lines changed

solution/3600-3699/3638.Maximum Balanced Shipments/README.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Ma
8787

8888
<!-- solution:start -->
8989

90-
### 方法一
90+
### 方法一:贪心
91+
92+
我们维护当前遍历的数组的最大值 $\text{mx}$,并遍历数组中的每个元素 $x$。如果 $x < \text{mx}$,则说明当前元素可以作为一个平衡装运的最后一个包裹,因此我们就将答案加一,并将 $\text{mx}$ 重置为 0。否则,我们更新 $\text{mx}$ 为当前元素 $x$ 的值。
93+
94+
遍历结束后,返回答案即可。
95+
96+
时间复杂度 $O(n)$,其中 $n$ 是数组的长度。空间复杂度 $O(1)$,只使用了常数级别的额外空间。
9197

9298
<!-- tabs:start -->
9399

94100
#### Python3
95101

96102
```python
97-
103+
class Solution:
104+
def maxBalancedShipments(self, weight: List[int]) -> int:
105+
ans = mx = 0
106+
for x in weight:
107+
mx = max(mx, x)
108+
if x < mx:
109+
ans += 1
110+
mx = 0
111+
return ans
98112
```
99113

100114
#### Java
101115

102116
```java
103-
117+
class Solution {
118+
public int maxBalancedShipments(int[] weight) {
119+
int ans = 0;
120+
int mx = 0;
121+
for (int x : weight) {
122+
mx = Math.max(mx, x);
123+
if (x < mx) {
124+
++ans;
125+
mx = 0;
126+
}
127+
}
128+
return ans;
129+
}
130+
}
104131
```
105132

106133
#### C++
107134

108135
```cpp
109-
136+
class Solution {
137+
public:
138+
int maxBalancedShipments(vector<int>& weight) {
139+
int ans = 0;
140+
int mx = 0;
141+
for (int x : weight) {
142+
mx = max(mx, x);
143+
if (x < mx) {
144+
++ans;
145+
mx = 0;
146+
}
147+
}
148+
return ans;
149+
}
150+
};
110151
```
111152
112153
#### Go
113154
114155
```go
156+
func maxBalancedShipments(weight []int) (ans int) {
157+
mx := 0
158+
for _, x := range weight {
159+
mx = max(mx, x)
160+
if x < mx {
161+
ans++
162+
mx = 0
163+
}
164+
}
165+
return
166+
}
167+
```
115168

169+
#### TypeScript
170+
171+
```ts
172+
function maxBalancedShipments(weight: number[]): number {
173+
let [ans, mx] = [0, 0];
174+
for (const x of weight) {
175+
mx = Math.max(mx, x);
176+
if (x < mx) {
177+
ans++;
178+
mx = 0;
179+
}
180+
}
181+
return ans;
182+
}
116183
```
117184

118185
<!-- tabs:end -->

solution/3600-3699/3638.Maximum Balanced Shipments/README_EN.md

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,32 +85,99 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3638.Ma
8585

8686
<!-- solution:start -->
8787

88-
### Solution 1
88+
### Solution 1: Greedy
89+
90+
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$.
91+
92+
After the traversal, we return the answer.
93+
94+
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.
8995

9096
<!-- tabs:start -->
9197

9298
#### Python3
9399

94100
```python
95-
101+
class Solution:
102+
def maxBalancedShipments(self, weight: List[int]) -> int:
103+
ans = mx = 0
104+
for x in weight:
105+
mx = max(mx, x)
106+
if x < mx:
107+
ans += 1
108+
mx = 0
109+
return ans
96110
```
97111

98112
#### Java
99113

100114
```java
101-
115+
class Solution {
116+
public int maxBalancedShipments(int[] weight) {
117+
int ans = 0;
118+
int mx = 0;
119+
for (int x : weight) {
120+
mx = Math.max(mx, x);
121+
if (x < mx) {
122+
++ans;
123+
mx = 0;
124+
}
125+
}
126+
return ans;
127+
}
128+
}
102129
```
103130

104131
#### C++
105132

106133
```cpp
107-
134+
class Solution {
135+
public:
136+
int maxBalancedShipments(vector<int>& weight) {
137+
int ans = 0;
138+
int mx = 0;
139+
for (int x : weight) {
140+
mx = max(mx, x);
141+
if (x < mx) {
142+
++ans;
143+
mx = 0;
144+
}
145+
}
146+
return ans;
147+
}
148+
};
108149
```
109150
110151
#### Go
111152
112153
```go
154+
func maxBalancedShipments(weight []int) (ans int) {
155+
mx := 0
156+
for _, x := range weight {
157+
mx = max(mx, x)
158+
if x < mx {
159+
ans++
160+
mx = 0
161+
}
162+
}
163+
return
164+
}
165+
```
113166

167+
#### TypeScript
168+
169+
```ts
170+
function maxBalancedShipments(weight: number[]): number {
171+
let [ans, mx] = [0, 0];
172+
for (const x of weight) {
173+
mx = Math.max(mx, x);
174+
if (x < mx) {
175+
ans++;
176+
mx = 0;
177+
}
178+
}
179+
return ans;
180+
}
114181
```
115182

116183
<!-- tabs:end -->
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxBalancedShipments(vector<int>& weight) {
4+
int ans = 0;
5+
int mx = 0;
6+
for (int x : weight) {
7+
mx = max(mx, x);
8+
if (x < mx) {
9+
++ans;
10+
mx = 0;
11+
}
12+
}
13+
return ans;
14+
}
15+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func maxBalancedShipments(weight []int) (ans int) {
2+
mx := 0
3+
for _, x := range weight {
4+
mx = max(mx, x)
5+
if x < mx {
6+
ans++
7+
mx = 0
8+
}
9+
}
10+
return
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int maxBalancedShipments(int[] weight) {
3+
int ans = 0;
4+
int mx = 0;
5+
for (int x : weight) {
6+
mx = Math.max(mx, x);
7+
if (x < mx) {
8+
++ans;
9+
mx = 0;
10+
}
11+
}
12+
return ans;
13+
}
14+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def maxBalancedShipments(self, weight: List[int]) -> int:
3+
ans = mx = 0
4+
for x in weight:
5+
mx = max(mx, x)
6+
if x < mx:
7+
ans += 1
8+
mx = 0
9+
return ans
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function maxBalancedShipments(weight: number[]): number {
2+
let [ans, mx] = [0, 0];
3+
for (const x of weight) {
4+
mx = Math.max(mx, x);
5+
if (x < mx) {
6+
ans++;
7+
mx = 0;
8+
}
9+
}
10+
return ans;
11+
}

0 commit comments

Comments
 (0)