Skip to content

Commit dccdcd5

Browse files
authored
Update 1425-constrained-subsequence-sum.js
1 parent 9827f03 commit dccdcd5

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

1425-constrained-subsequence-sum.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,10 @@ const constrainedSubsetSum = function (nums, k) {
3333
dll.push([0, nums[0]])
3434
let max = nums[0]
3535
for (let i = 1; i < nums.length; i++) {
36-
// console.log(dll, dll.peek())
37-
let [index, lastKsum] = dll.peek().val
38-
39-
if (index == i - k) {
36+
if (!dll.isEmpty() && i - dll.peek().val[0] > k) {
4037
dll.shift()
4138
}
42-
const sum = Math.max(lastKsum, 0) + nums[i]
39+
const sum = Math.max(dll.peek().val[1], 0) + nums[i]
4340
max = Math.max(max, sum)
4441
while (!dll.isEmpty() && dll.peekLast().val[1] < sum) {
4542
dll.pop()
@@ -60,6 +57,7 @@ class DLL {
6057
constructor() {
6158
this.head = new Node()
6259
this.tail = null
60+
this.size = 0
6361
}
6462
peek() {
6563
return this.head.next
@@ -79,6 +77,7 @@ class DLL {
7977
} else {
8078
this.tail = null
8179
}
80+
this.size--
8281
}
8382
}
8483
pop() {
@@ -88,12 +87,15 @@ class DLL {
8887
newTail.next = null
8988
this.tail.prev = null
9089
this.tail = newTail
90+
this.size--
9191
}
92+
9293
}
9394
push(val) {
9495
const node = new Node(val)
9596
node.prev = this.tail ?? this.head
9697
node.prev.next = node
9798
this.tail = node
99+
this.size++
98100
}
99101
}

0 commit comments

Comments
 (0)