forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0239-sliding-window-maximum.js
139 lines (129 loc) · 3.34 KB
/
0239-sliding-window-maximum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
function Node(value) {
this.value = value;
this.prev = null;
this.next = null;
}
function Deque() {
this.left = null;
this.right = null;
this.size = 0;
this.pushRight = function (value) {
const node = new Node(value);
if (this.size == 0) {
this.left = node;
this.right = node;
} else {
this.right.next = node;
node.prev = this.right;
this.right = node;
}
this.size++;
return this.size;
};
this.popRight = function () {
if (this.size == 0) return null;
const removedNode = this.right;
this.right = this.right.prev;
if (this.right) this.right.next = null;
this.size--;
return removedNode;
};
this.pushLeft = function (value) {
const node = new Node(value);
if (this.size == 0) {
this.left = node;
this.right = node;
} else {
this.left.prev = node;
node.next = this.left;
this.left = node;
}
this.size++;
return this.size;
};
this.popLeft = function () {
if (this.size == 0) return null;
const removedNode = this.left;
this.left = this.left.next;
if (this.left) this.left.prev = null;
this.size--;
return removedNode;
};
}
var maxSlidingWindow = function (nums, k) {
const output = [];
let deque = new Deque();
let left = 0;
let right = 0;
while (right < nums.length) {
// pop smaller values from q
while (deque.right && nums[deque.right.value] < nums[right])
deque.popRight();
deque.pushRight(right);
// remove left val from window
if (left > deque.left.value) deque.popLeft();
if (right + 1 >= k) {
output.push(nums[deque.left.value]);
left++;
}
right++;
}
return output;
};
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
// Deque Implementation using Lazy Deletion
class LazyDeletionDeque {
constructor() {
this.deque = [];
this.leftIdx = 0;
}
isEmpty = () => {
return this.deque.length === this.leftIdx;
};
push = (num) => {
this.deque.push(num);
};
popFront = () => {
this.leftIdx++;
};
popBack = () => {
!this.isEmpty() && this.deque.pop();
};
front = () => {
return this.deque[this.leftIdx];
};
back = () => {
return this.deque[this.deque.length - 1];
};
}
var maxSlidingWindowWithLazyDeletionDeque = function (nums, k) {
const deque = new LazyDeletionDeque();
const answer = [];
let leftWindow = 0;
for (let rightWindow = 0; rightWindow < nums.length; rightWindow++) {
const rightNum = nums[rightWindow];
while (!deque.isEmpty() && rightNum > deque.back()) {
deque.popBack();
}
deque.push(rightNum);
if (rightWindow >= k - 1) {
const dequeFront = deque.front();
const leftNum = nums[leftWindow];
if (leftNum === dequeFront) {
deque.popFront();
}
answer.push(dequeFront);
leftWindow++;
}
}
return answer;
};