72
72
73
73
## 解法
74
74
75
- ### 方法一
75
+ ### 方法一:双指针 + 模拟
76
+
77
+ 我们用两个变量 $a$ 和 $b$ 分别表示 Alice 和 Bob 的水量,初始时 $a = \text{capacityA}$, $b = \text{capacityB}$。然后用两个指针 $i$ 和 $j$ 分别指向植物数组的头尾,然后模拟 Alice 和 Bob 从两端向中间浇水的过程。
78
+
79
+ 当 $i < j$ 时,我们分别判断 Alice 和 Bob 的水量是否足够浇水,如果不够,我们就重新灌满水罐。然后更新 $a$ 和 $b$ 的水量,同时移动指针 $i$ 和 $j$。最后我们还需要判断 $i$ 和 $j$ 是否相等,如果相等,我们还需要判断 $\max(a, b)$ 是否小于植物的水量,如果小于,我们需要再次重新灌满水罐。
80
+
81
+ 时间复杂度 $O(n)$,其中 $n$ 是植物数组的长度。空间复杂度 $O(1)$。
76
82
77
83
<!-- tabs:start -->
78
84
79
85
``` python
80
86
class Solution :
81
87
def minimumRefill (self , plants : List[int ], capacityA : int , capacityB : int ) -> int :
82
- i, j = 0 , len (plants) - 1
83
- ans = 0
84
88
a, b = capacityA, capacityB
85
- while i <= j:
86
- if i == j:
87
- if max (capacityA, capacityB) < plants[i]:
88
- ans += 1
89
- break
90
- if capacityA < plants[i]:
91
- capacityA = a - plants[i]
89
+ ans = 0
90
+ i, j = 0 , len (plants) - 1
91
+ while i < j:
92
+ if a < plants[i]:
92
93
ans += 1
93
- else :
94
- capacityA -= plants[i]
95
- if capacityB < plants[j]:
96
- capacityB = b - plants[j]
94
+ a = capacityA
95
+ a -= plants[i]
96
+ if b < plants[j]:
97
97
ans += 1
98
- else :
99
- capacityB -= plants[j]
100
- i += 1
101
- j -= 1
98
+ b = capacityB
99
+ b -= plants[j]
100
+ i, j = i + 1 , j - 1
101
+ ans += i == j and max (a, b) < plants[i]
102
102
return ans
103
103
```
104
104
105
105
``` java
106
106
class Solution {
107
107
public int minimumRefill (int [] plants , int capacityA , int capacityB ) {
108
+ int a = capacityA, b = capacityB;
109
+ int ans = 0 ;
108
110
int i = 0 , j = plants. length - 1 ;
109
- int ans = 0 , a = capacityA, b = capacityB;
110
- while (i <= j) {
111
- if (i == j) {
112
- if (Math . max(capacityA, capacityB) < plants[i]) {
113
- ++ ans;
114
- }
115
- break ;
116
- }
117
- if (capacityA < plants[i]) {
118
- capacityA = a - plants[i];
111
+ for (; i < j; ++ i, -- j) {
112
+ if (a < plants[i]) {
119
113
++ ans;
120
- } else {
121
- capacityA -= plants[i];
114
+ a = capacityA;
122
115
}
123
- if (capacityB < plants[j]) {
124
- capacityB = b - plants[j];
116
+ a -= plants[i];
117
+ if (b < plants[j]) {
125
118
++ ans;
126
- } else {
127
- capacityB -= plants[j];
119
+ b = capacityB;
128
120
}
129
- ++ i;
130
- -- j;
121
+ b -= plants[j];
131
122
}
123
+ ans += i == j && Math . max(a, b) < plants[i] ? 1 : 0 ;
132
124
return ans;
133
125
}
134
126
}
@@ -138,59 +130,104 @@ class Solution {
138
130
class Solution {
139
131
public:
140
132
int minimumRefill(vector<int >& plants, int capacityA, int capacityB) {
133
+ int a = capacityA, b = capacityB;
134
+ int ans = 0;
141
135
int i = 0, j = plants.size() - 1;
142
- int ans = 0, a = capacityA, b = capacityB;
143
- while (i <= j) {
144
- if (i == j) {
145
- if (max(capacityA, capacityB) < plants[ i] ) ++ans;
146
- break;
147
- }
148
- if (capacityA < plants[ i] ) {
149
- capacityA = a - plants[ i] ;
136
+ for (; i < j; ++i, --j) {
137
+ if (a < plants[ i] ) {
150
138
++ans;
151
- } else
152
- capacityA -= plants[ i] ;
153
-
154
- if (capacityB < plants[j]) {
155
- capacityB = b - plants[j];
139
+ a = capacityA;
140
+ }
141
+ a -= plants[ i] ;
142
+ if (b < plants[ j] ) {
156
143
++ans;
157
- } else
158
- capacityB -= plants[j];
159
- ++i;
160
- --j;
144
+ b = capacityB;
145
+ }
146
+ b -= plants[ j] ;
161
147
}
148
+ ans += i == j && max(a, b) < plants[ i] ;
162
149
return ans;
163
150
}
164
151
};
165
152
```
166
153
167
154
```go
168
- func minimumRefill (plants []int , capacityA int , capacityB int ) int {
155
+ func minimumRefill(plants []int, capacityA int, capacityB int) (ans int) {
156
+ a, b := capacityA, capacityB
169
157
i, j := 0, len(plants)-1
170
- ans , a , b := 0 , capacityA, capacityB
171
- for i <= j {
172
- if i == j {
173
- if max (capacityA, capacityB) < plants[i] {
174
- ans++
175
- }
176
- break
177
- }
178
- if capacityA < plants[i] {
179
- capacityA = a - plants[i]
158
+ for ; i < j; i, j = i+1, j-1 {
159
+ if a < plants[i] {
180
160
ans++
181
- } else {
182
- capacityA -= plants[i]
161
+ a = capacityA
183
162
}
184
- if capacityB < plants[j] {
185
- capacityB = b - plants[j]
163
+ a -= plants[i]
164
+ if b < plants[j] {
186
165
ans++
187
- } else {
188
- capacityB -= plants[j]
166
+ b = capacityB
189
167
}
190
- i++
191
- j--
168
+ b -= plants[j]
169
+ }
170
+ if i == j && max(a, b) < plants[i] {
171
+ ans++
192
172
}
193
- return ans
173
+ return
174
+ }
175
+ ```
176
+
177
+ ``` ts
178
+ function minimumRefill(plants : number [], capacityA : number , capacityB : number ): number {
179
+ let [a, b] = [capacityA , capacityB ];
180
+ let ans = 0 ;
181
+ let [i, j] = [0 , plants .length - 1 ];
182
+ for (; i < j ; ++ i , -- j ) {
183
+ if (a < plants [i ]) {
184
+ ++ ans ;
185
+ a = capacityA ;
186
+ }
187
+ a -= plants [i ];
188
+ if (b < plants [j ]) {
189
+ ++ ans ;
190
+ b = capacityB ;
191
+ }
192
+ b -= plants [j ];
193
+ }
194
+ ans += i === j && Math .max (a , b ) < plants [i ] ? 1 : 0 ;
195
+ return ans ;
196
+ }
197
+ ```
198
+
199
+ ``` rust
200
+ impl Solution {
201
+ pub fn minimum_refill (plants : Vec <i32 >, capacity_a : i32 , capacity_b : i32 ) -> i32 {
202
+ let mut a = capacity_a ;
203
+ let mut b = capacity_b ;
204
+ let mut ans = 0 ;
205
+ let mut i = 0 ;
206
+ let mut j = plants . len () - 1 ;
207
+
208
+ while i < j {
209
+ if a < plants [i ] {
210
+ ans += 1 ;
211
+ a = capacity_a ;
212
+ }
213
+ a -= plants [i ];
214
+
215
+ if b < plants [j ] {
216
+ ans += 1 ;
217
+ b = capacity_b ;
218
+ }
219
+ b -= plants [j ];
220
+
221
+ i += 1 ;
222
+ j -= 1 ;
223
+ }
224
+
225
+ if i == j && a . max (b ) < plants [i ] {
226
+ ans += 1 ;
227
+ }
228
+
229
+ ans
230
+ }
194
231
}
195
232
```
196
233
0 commit comments