forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.05 KB
/
index.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
/**
You must implement the nextGreaterElement() function.
For each element in an array, it finds the next greater element in that array.
To keep it simple, the next greater element for the last or maximum value in the array is -1.
Input: [4, 6, 3, 2, 8, 1]
Output: [6, 8, 8, 8, -1, -1]
*/
const Stack = require('../../_DataStructures_/Stack');
function nextGreaterElement(arr) {
if (!Array.isArray(arr)) {
throw new Error('Invalid Argument');
}
const s1 = new Stack();
const nextGreater = [];
for (let i = arr.length - 1; i >= 0; i -= 1) {
if (s1.peek()) {
let top = s1.peek();
while (top <= arr[i]) {
// if the stack is empty, break the while loop
if (!s1.peek()) break;
// pop the elements
s1.pop();
// get the new top
top = s1.peek();
}
}
if (s1.peek()) {
nextGreater[i] = s1.peek();
} else {
nextGreater[i] = -1;
}
// push the element into the stack
s1.push(arr[i]);
}
return nextGreater;
}
module.exports = { nextGreaterElement };