-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnextGreaterElement.js
More file actions
executable file
·41 lines (30 loc) · 1.21 KB
/
nextGreaterElement.js
File metadata and controls
executable file
·41 lines (30 loc) · 1.21 KB
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
// function to find next greater element for each element in the array
// what is next greater element ?
// next greater element for an element x is the first greater element on the right side of x in the array.
// For example, the next greater element for 4 in the array [4, 5, 2, 10] is 5.
// If there is no greater element for an element, we can consider the next greater element as -1.
function nextGreaterElement(arr) {
const result = new Array(arr.length).fill(-1);
const stack = [];
for (let i = 0; i < arr.length; i++) {
while (stack.length > 0 && arr[i] > arr[stack[stack.length - 1]]) {
const index = stack.pop();
result[index] = arr[i];
}
stack.push(i);
}
return result;
}
// time complexity: O(n)
// space complexity: O(n) for the stack and result array
// test the function
console.log(nextGreaterElement([4, 5, 2, 10]));
// Output: [ 5, 10, 10, -1 ]
console.log(nextGreaterElement([3, 7, 1, 9, 5]));
// Output: [ 7, 9, 9, -1, -1 ]
console.log(nextGreaterElement([1, 2, 3, 4, 5]));
// Output: [ 2, 3, 4, 5, -1 ]
console.log(nextGreaterElement([5, 4, 3, 2, 1]));
// Output: [ -1, -1, -1, -1, -1 ]
console.log(nextGreaterElement([2, 1, 2, 4, 3]));
// Output: [ 4, 2, 4, -1, -1 ]