@@ -113,9 +113,13 @@ public:
113
113
f[ 0] = 0;
114
114
deque<int > q = {0};
115
115
for (int i = 0; i < n; ++i) {
116
- if (i - q.front() > k) q.pop_front();
116
+ if (i - q.front() > k) {
117
+ q.pop_front();
118
+ }
117
119
f[ i] = nums[ i] + f[ q.front()] ;
118
- while (!q.empty() && f[ q.back()] <= f[ i] ) q.pop_back();
120
+ while (!q.empty() && f[ i] >= f[ q.back()] ) {
121
+ q.pop_back();
122
+ }
119
123
q.push_back(i);
120
124
}
121
125
return f[ n - 1] ;
@@ -127,19 +131,193 @@ public:
127
131
func maxResult(nums []int, k int) int {
128
132
n := len(nums)
129
133
f := make([]int, n)
130
- q := []int{0}
131
- for i, v := range nums {
132
- if i-q[0] > k {
133
- q = q[1:]
134
+ q := Deque{}
135
+ q.PushBack(0)
136
+ for i := 0; i < n; i++ {
137
+ if i-q.Front() > k {
138
+ q.PopFront()
134
139
}
135
- f[i] = v + f[q[0] ]
136
- for len(q) > 0 && f[q[len(q)-1]] < = f[i ] {
137
- q = q[:len(q)-1]
140
+ f[i] = nums[i] + f[q.Front() ]
141
+ for !q.Empty() && f[i] > = f[q.Back() ] {
142
+ q.PopBack()
138
143
}
139
- q = append(q, i)
144
+ q.PushBack( i)
140
145
}
141
146
return f[n-1]
142
147
}
148
+
149
+ type Deque struct{ l, r []int }
150
+
151
+ func (q Deque) Empty() bool {
152
+ return len(q.l) == 0 && len(q.r) == 0
153
+ }
154
+
155
+ func (q Deque) Size() int {
156
+ return len(q.l) + len(q.r)
157
+ }
158
+
159
+ func (q *Deque) PushFront(v int) {
160
+ q.l = append(q.l, v)
161
+ }
162
+
163
+ func (q *Deque) PushBack(v int) {
164
+ q.r = append(q.r, v)
165
+ }
166
+
167
+ func (q *Deque) PopFront() (v int) {
168
+ if len(q.l) > 0 {
169
+ q.l, v = q.l[:len(q.l)-1], q.l[len(q.l)-1]
170
+ } else {
171
+ v, q.r = q.r[0], q.r[1:]
172
+ }
173
+ return
174
+ }
175
+
176
+ func (q *Deque) PopBack() (v int) {
177
+ if len(q.r) > 0 {
178
+ q.r, v = q.r[:len(q.r)-1], q.r[len(q.r)-1]
179
+ } else {
180
+ v, q.l = q.l[0], q.l[1:]
181
+ }
182
+ return
183
+ }
184
+
185
+ func (q Deque) Front() int {
186
+ if len(q.l) > 0 {
187
+ return q.l[len(q.l)-1]
188
+ }
189
+ return q.r[0]
190
+ }
191
+
192
+ func (q Deque) Back() int {
193
+ if len(q.r) > 0 {
194
+ return q.r[len(q.r)-1]
195
+ }
196
+ return q.l[0]
197
+ }
198
+
199
+ func (q Deque) Get(i int) int {
200
+ if i < len(q.l) {
201
+ return q.l[len(q.l)-1-i]
202
+ }
203
+ return q.r[i-len(q.l)]
204
+ }
205
+ ```
206
+
207
+ ``` ts
208
+ function maxResult(nums : number [], k : number ): number {
209
+ const n = nums .length ;
210
+ const f: number [] = Array (n ).fill (0 );
211
+ const q = new Deque <number >();
212
+ q .pushBack (0 );
213
+ for (let i = 0 ; i < n ; ++ i ) {
214
+ if (i - q .frontValue ()! > k ) {
215
+ q .popFront ();
216
+ }
217
+ f [i ] = nums [i ] + f [q .frontValue ()! ];
218
+ while (! q .isEmpty () && f [i ] >= f [q .backValue ()! ]) {
219
+ q .popBack ();
220
+ }
221
+ q .pushBack (i );
222
+ }
223
+ return f [n - 1 ];
224
+ }
225
+
226
+ class Node <T > {
227
+ value: T ;
228
+ next: Node <T > | null ;
229
+ prev: Node <T > | null ;
230
+
231
+ constructor (value : T ) {
232
+ this .value = value ;
233
+ this .next = null ;
234
+ this .prev = null ;
235
+ }
236
+ }
237
+
238
+ class Deque <T > {
239
+ private front: Node <T > | null ;
240
+ private back: Node <T > | null ;
241
+ private size: number ;
242
+
243
+ constructor () {
244
+ this .front = null ;
245
+ this .back = null ;
246
+ this .size = 0 ;
247
+ }
248
+
249
+ pushFront(val : T ): void {
250
+ const newNode = new Node (val );
251
+ if (this .isEmpty ()) {
252
+ this .front = newNode ;
253
+ this .back = newNode ;
254
+ } else {
255
+ newNode .next = this .front ;
256
+ this .front ! .prev = newNode ;
257
+ this .front = newNode ;
258
+ }
259
+ this .size ++ ;
260
+ }
261
+
262
+ pushBack(val : T ): void {
263
+ const newNode = new Node (val );
264
+ if (this .isEmpty ()) {
265
+ this .front = newNode ;
266
+ this .back = newNode ;
267
+ } else {
268
+ newNode .prev = this .back ;
269
+ this .back ! .next = newNode ;
270
+ this .back = newNode ;
271
+ }
272
+ this .size ++ ;
273
+ }
274
+
275
+ popFront(): T | undefined {
276
+ if (this .isEmpty ()) {
277
+ return undefined ;
278
+ }
279
+ const value = this .front ! .value ;
280
+ this .front = this .front ! .next ;
281
+ if (this .front !== null ) {
282
+ this .front .prev = null ;
283
+ } else {
284
+ this .back = null ;
285
+ }
286
+ this .size -- ;
287
+ return value ;
288
+ }
289
+
290
+ popBack(): T | undefined {
291
+ if (this .isEmpty ()) {
292
+ return undefined ;
293
+ }
294
+ const value = this .back ! .value ;
295
+ this .back = this .back ! .prev ;
296
+ if (this .back !== null ) {
297
+ this .back .next = null ;
298
+ } else {
299
+ this .front = null ;
300
+ }
301
+ this .size -- ;
302
+ return value ;
303
+ }
304
+
305
+ frontValue(): T | undefined {
306
+ return this .front ?.value ;
307
+ }
308
+
309
+ backValue(): T | undefined {
310
+ return this .back ?.value ;
311
+ }
312
+
313
+ getSize(): number {
314
+ return this .size ;
315
+ }
316
+
317
+ isEmpty(): boolean {
318
+ return this .size === 0 ;
319
+ }
320
+ }
143
321
```
144
322
145
323
<!-- tabs: end -->
0 commit comments