| 1 | class MonotonicStack { |
| 2 | nextGreaterElement(nums) { |
| 3 | let n = nums.length; |
| 4 | let result = new Array(n).fill(-1); // Default to -1 if no greater element exists |
| 5 | let stack = []; // Stack stores indices |
| 6 | |
| 7 | for (let i = 0; i < n; i++) { |
| 8 | while (stack.length > 0 && nums[i] > nums[stack[stack.length - 1]]) { |
| 9 | let index = stack.pop(); |
| 10 | result[index] = nums[i]; |
| 11 | } |
| 12 | stack.push(i); |
| 13 | } |
| 14 | return result; |
| 15 | } |
| 16 | |
| 17 | dailyTemperatures(temperatures) { |
| 18 | let n = temperatures.length; |
| 19 | let result = new Array(n).fill(0); // Result array initialized with 0s |
| 20 | let stack = []; // Monotonic decreasing stack |
| 21 | |
| 22 | for (let i = 0; i < n; i++) { |
| 23 | while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) { |
| 24 | let prevIndex = stack.pop(); |
| 25 | result[prevIndex] = i - prevIndex; |
| 26 | } |
| 27 | stack.push(i); |
| 28 | } |
| 29 | return result; |
| 30 | } |
| 31 | } |
nothing calls this directly
no outgoing calls
no test coverage detected