@@ -87,18 +87,18 @@ class Solution:
87
87
self , chargeTimes : List[int ], runningCosts : List[int ], budget : int
88
88
) -> int :
89
89
q = deque()
90
- ans = j = s = 0
91
- for i, (a, b) in enumerate (zip (chargeTimes, runningCosts)):
92
- while q and chargeTimes[q[- 1 ]] <= a:
90
+ ans = s = l = 0
91
+ for r, (t, c) in enumerate (zip (chargeTimes, runningCosts)):
92
+ s += c
93
+ while q and chargeTimes[q[- 1 ]] <= t:
93
94
q.pop()
94
- q.append(i)
95
- s += b
96
- while q and chargeTimes[q[0 ]] + (i - j + 1 ) * s > budget:
97
- if q[0 ] == j:
95
+ q.append(r)
96
+ while q and (r - l + 1 ) * s + chargeTimes[q[0 ]] > budget:
97
+ if q[0 ] == l:
98
98
q.popleft()
99
- s -= runningCosts[j ]
100
- j += 1
101
- ans = max (ans, i - j + 1 )
99
+ s -= runningCosts[l ]
100
+ l += 1
101
+ ans = max (ans, r - l + 1 )
102
102
return ans
103
103
```
104
104
@@ -109,23 +109,21 @@ class Solution {
109
109
public int maximumRobots (int [] chargeTimes , int [] runningCosts , long budget ) {
110
110
Deque<Integer > q = new ArrayDeque<> ();
111
111
int n = chargeTimes. length;
112
- long s = 0 ;
113
- int j = 0 ;
114
112
int ans = 0 ;
115
- for (int i = 0 ; i < n; ++ i) {
116
- int a = chargeTimes[i], b = runningCosts[i];
117
- while (! q. isEmpty() && chargeTimes[q. getLast()] <= a) {
113
+ long s = 0 ;
114
+ for (int l = 0 , r = 0 ; r < n; ++ r) {
115
+ s += runningCosts[r];
116
+ while (! q. isEmpty() && chargeTimes[q. peekLast()] <= chargeTimes[r]) {
118
117
q. pollLast();
119
118
}
120
- q. offer(i);
121
- s += b;
122
- while (! q. isEmpty() && chargeTimes[q. getFirst()] + (i - j + 1 ) * s > budget) {
123
- if (q. getFirst() == j) {
119
+ q. offerLast(r);
120
+ while (! q. isEmpty() && (r - l + 1 ) * s + chargeTimes[q. peekFirst()] > budget) {
121
+ if (q. peekFirst() == l) {
124
122
q. pollFirst();
125
123
}
126
- s -= runningCosts[j ++ ];
124
+ s -= runningCosts[l ++ ];
127
125
}
128
- ans = Math . max(ans, i - j + 1 );
126
+ ans = Math . max(ans, r - l + 1 );
129
127
}
130
128
return ans;
131
129
}
@@ -140,19 +138,21 @@ public:
140
138
int maximumRobots(vector<int >& chargeTimes, vector<int >& runningCosts, long long budget) {
141
139
deque<int > q;
142
140
long long s = 0;
143
- int ans = 0, j = 0, n = chargeTimes.size();
144
- for (int i = 0; i < n; ++i) {
145
- int a = chargeTimes[ i] , b = runningCosts[ i] ;
146
- while (!q.empty() && chargeTimes[ q.back()] <= a) q.pop_back();
147
- q.push_back(i);
148
- s += b;
149
- while (!q.empty() && chargeTimes[ q.front()] + (i - j + 1) * s > budget) {
150
- if (q.front() == j) {
141
+ int ans = 0;
142
+ int n = chargeTimes.size();
143
+ for (int l = 0, r = 0; r < n; ++r) {
144
+ s += runningCosts[ r] ;
145
+ while (q.size() && chargeTimes[ q.back()] <= chargeTimes[ r] ) {
146
+ q.pop_back();
147
+ }
148
+ q.push_back(r);
149
+ while (q.size() && (r - l + 1) * s + chargeTimes[ q.front()] > budget) {
150
+ if (q.front() == l) {
151
151
q.pop_front();
152
152
}
153
- s -= runningCosts[ j ++] ;
153
+ s -= runningCosts[ l ++] ;
154
154
}
155
- ans = max(ans, i - j + 1);
155
+ ans = max(ans, r - l + 1);
156
156
}
157
157
return ans;
158
158
}
@@ -162,26 +162,205 @@ public:
162
162
#### Go
163
163
164
164
```go
165
- func maximumRobots(chargeTimes []int, runningCosts []int, budget int64) int {
165
+ func maximumRobots(chargeTimes []int, runningCosts []int, budget int64) (ans int) {
166
+ q := Deque{}
166
167
s := int64(0)
167
- ans, j := 0, 0
168
- q := []int{}
169
- for i, a := range chargeTimes {
170
- for len(q) > 0 && chargeTimes[q[len(q)-1]] <= a {
171
- q = q[:len(q)-1]
168
+ l := 0
169
+ for r, t := range chargeTimes {
170
+ s += int64(runningCosts[r])
171
+ for !q.Empty() && chargeTimes[q.Back()] <= t {
172
+ q.PopBack()
172
173
}
173
- q = append(q, i)
174
- s += int64(runningCosts[i])
175
- for len(q) > 0 && int64(chargeTimes[q[0]])+int64(i-j+1)*s > budget {
176
- if q[0] == j {
177
- q = q[1:]
174
+ q.PushBack(r)
175
+ for !q.Empty() && int64(r-l+1)*s+int64(chargeTimes[q.Front()]) > budget {
176
+ if q.Front() == l {
177
+ q.PopFront()
178
178
}
179
- s -= int64(runningCosts[j ])
180
- j ++
179
+ s -= int64(runningCosts[l ])
180
+ l ++
181
181
}
182
- ans = max(ans, i-j +1)
182
+ ans = max(ans, r-l +1)
183
183
}
184
- return ans
184
+ return
185
+ }
186
+
187
+ // template
188
+ type Deque struct{ l, r []int }
189
+
190
+ func (q Deque) Empty() bool {
191
+ return len(q.l) == 0 && len(q.r) == 0
192
+ }
193
+
194
+ func (q Deque) Size() int {
195
+ return len(q.l) + len(q.r)
196
+ }
197
+
198
+ func (q *Deque) PushFront(v int) {
199
+ q.l = append(q.l, v)
200
+ }
201
+
202
+ func (q *Deque) PushBack(v int) {
203
+ q.r = append(q.r, v)
204
+ }
205
+
206
+ func (q *Deque) PopFront() (v int) {
207
+ if len(q.l) > 0 {
208
+ q.l, v = q.l[:len(q.l)-1], q.l[len(q.l)-1]
209
+ } else {
210
+ v, q.r = q.r[0], q.r[1:]
211
+ }
212
+ return
213
+ }
214
+
215
+ func (q *Deque) PopBack() (v int) {
216
+ if len(q.r) > 0 {
217
+ q.r, v = q.r[:len(q.r)-1], q.r[len(q.r)-1]
218
+ } else {
219
+ v, q.l = q.l[0], q.l[1:]
220
+ }
221
+ return
222
+ }
223
+
224
+ func (q Deque) Front() int {
225
+ if len(q.l) > 0 {
226
+ return q.l[len(q.l)-1]
227
+ }
228
+ return q.r[0]
229
+ }
230
+
231
+ func (q Deque) Back() int {
232
+ if len(q.r) > 0 {
233
+ return q.r[len(q.r)-1]
234
+ }
235
+ return q.l[0]
236
+ }
237
+
238
+ func (q Deque) Get(i int) int {
239
+ if i < len(q.l) {
240
+ return q.l[len(q.l)-1-i]
241
+ }
242
+ return q.r[i-len(q.l)]
243
+ }
244
+ ```
245
+
246
+ #### TypeScript
247
+
248
+ ``` ts
249
+ function maximumRobots(chargeTimes : number [], runningCosts : number [], budget : number ): number {
250
+ const q = new Deque <number >();
251
+ const n = chargeTimes .length ;
252
+ let [ans, s] = [0 , 0 ];
253
+ for (let l = 0 , r = 0 ; r < n ; ++ r ) {
254
+ s += runningCosts [r ];
255
+ while (! q .isEmpty () && chargeTimes [q .backValue ()! ] <= chargeTimes [r ]) {
256
+ q .popBack ();
257
+ }
258
+ q .pushBack (r );
259
+ while (! q .isEmpty () && (r - l + 1 ) * s + chargeTimes [q .frontValue ()! ] > budget ) {
260
+ if (q .frontValue () === l ) {
261
+ q .popFront ();
262
+ }
263
+ s -= runningCosts [l ++ ];
264
+ }
265
+ ans = Math .max (ans , r - l + 1 );
266
+ }
267
+ return ans ;
268
+ }
269
+
270
+ class Node <T > {
271
+ value: T ;
272
+ next: Node <T > | null ;
273
+ prev: Node <T > | null ;
274
+
275
+ constructor (value : T ) {
276
+ this .value = value ;
277
+ this .next = null ;
278
+ this .prev = null ;
279
+ }
280
+ }
281
+
282
+ class Deque <T > {
283
+ private front: Node <T > | null ;
284
+ private back: Node <T > | null ;
285
+ private size: number ;
286
+
287
+ constructor () {
288
+ this .front = null ;
289
+ this .back = null ;
290
+ this .size = 0 ;
291
+ }
292
+
293
+ pushFront(val : T ): void {
294
+ const newNode = new Node (val );
295
+ if (this .isEmpty ()) {
296
+ this .front = newNode ;
297
+ this .back = newNode ;
298
+ } else {
299
+ newNode .next = this .front ;
300
+ this .front ! .prev = newNode ;
301
+ this .front = newNode ;
302
+ }
303
+ this .size ++ ;
304
+ }
305
+
306
+ pushBack(val : T ): void {
307
+ const newNode = new Node (val );
308
+ if (this .isEmpty ()) {
309
+ this .front = newNode ;
310
+ this .back = newNode ;
311
+ } else {
312
+ newNode .prev = this .back ;
313
+ this .back ! .next = newNode ;
314
+ this .back = newNode ;
315
+ }
316
+ this .size ++ ;
317
+ }
318
+
319
+ popFront(): T | undefined {
320
+ if (this .isEmpty ()) {
321
+ return undefined ;
322
+ }
323
+ const value = this .front ! .value ;
324
+ this .front = this .front ! .next ;
325
+ if (this .front !== null ) {
326
+ this .front .prev = null ;
327
+ } else {
328
+ this .back = null ;
329
+ }
330
+ this .size -- ;
331
+ return value ;
332
+ }
333
+
334
+ popBack(): T | undefined {
335
+ if (this .isEmpty ()) {
336
+ return undefined ;
337
+ }
338
+ const value = this .back ! .value ;
339
+ this .back = this .back ! .prev ;
340
+ if (this .back !== null ) {
341
+ this .back .next = null ;
342
+ } else {
343
+ this .front = null ;
344
+ }
345
+ this .size -- ;
346
+ return value ;
347
+ }
348
+
349
+ frontValue(): T | undefined {
350
+ return this .front ?.value ;
351
+ }
352
+
353
+ backValue(): T | undefined {
354
+ return this .back ?.value ;
355
+ }
356
+
357
+ getSize(): number {
358
+ return this .size ;
359
+ }
360
+
361
+ isEmpty(): boolean {
362
+ return this .size === 0 ;
363
+ }
185
364
}
186
365
```
187
366
0 commit comments