-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnextGreaterElementI.js
48 lines (42 loc) · 1.58 KB
/
nextGreaterElementI.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
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number[]}
*/
var nextGreaterElement = function(nums1, arr) {
let printArr = [];
let stack = [];
stack.push({index : 0 , value : arr[0]});
for (let itr = 1; itr < arr.length; itr++) {
let currentElement = arr[itr];
while(stack.length && currentElement > stack[stack.length-1].value){
let stackTop = stack.pop();
//console.log(stackTop.value+"--> "+currentElement);
printArr[stackTop.index] = {current : stackTop.value , nextGreaterElement : currentElement };
}
stack.push({ index : itr , value : currentElement});
}
while(stack.length) {
let stackTop = stack.pop();
let currentElement = -1
//console.log(stackTop.value+"--> "+currentElement);
printArr[stackTop.index] = {current : stackTop.value , nextGreaterElement : currentElement };
}
for (let itr = 0; itr < printArr.length; itr++) {
console.log(printArr[itr].current+"--> "+printArr[itr].nextGreaterElement);
}
let hasMap = {};
for(let itr = 0 ; itr < arr.length ; itr ++) {
hasMap[arr[itr]] = itr;
}
let output = [];
for(let itr = 0 ; itr < nums1.length ; itr ++) {
if(hasMap.hasOwnProperty(nums1[itr])) {
output.push(
printArr[hasMap[nums1[itr]]].nextGreaterElement
);
}
}
return output;
};
//https://leetcode.com/problems/next-greater-element-i/