-
Notifications
You must be signed in to change notification settings - Fork 2
/
239_sliding_window_max.js
56 lines (49 loc) · 1.02 KB
/
239_sliding_window_max.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
/*
This leetcode hard question was asked in Tech round 2 of prismforce
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var maxSlidingWindow = function (nums, k) {
const result = [];
let i = 0;
let j = 0;
let localMax = -Infinity;
while (i < nums.length && j <= nums.length) {
if (j < i + k) {
if (nums[j] > localMax) {
localMax = nums[j];
}
j++;
} else {
result.push(localMax);
localMax = -Infinity;
i++;
j = i;
}
}
return result;
};
// var maxSlidingWindow = function(nums, k) {
// const result = [];
// let i = 0;
// let localMax = -Infinity;
// while(i < k){
// if(nums[i] > localMax){
// localMax = nums[i];
// }
// i++;
// }
// result.push(localMax);
// i = k;
// while (i < nums.length){
// if(nums[i] > localMax){
// localMax = nums[i];
// }
// result.push(localMax);
// i++;
// }
// return result;
// };