-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathmonotonicStack.ts
31 lines (28 loc) · 1.08 KB
/
monotonicStack.ts
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
class MonotonicStack {
nextGreaterElement(nums: number[]): number[] {
let n = nums.length;
let result: number[] = new Array(n).fill(-1); // Default to -1 if no greater element exists
let stack: number[] = []; // Stack stores indices
for (let i = 0; i < n; i++) {
while (stack.length > 0 && nums[i] > nums[stack[stack.length - 1]]) {
let index = stack.pop()!;
result[index] = nums[i];
}
stack.push(i);
}
return result;
}
dailyTemperatures(temperatures: number[]): number[] {
let n = temperatures.length;
let result: number[] = new Array(n).fill(0); // Result array initialized with 0s
let stack: number[] = []; // Monotonic decreasing stack
for (let i = 0; i < n; i++) {
while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
let prevIndex = stack.pop()!;
result[prevIndex] = i - prevIndex;
}
stack.push(i);
}
return result;
}
}