-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path2832-maximal-range-that-each-element-is-maximum-in-it.js
More file actions
51 lines (45 loc) · 1.36 KB
/
2832-maximal-range-that-each-element-is-maximum-in-it.js
File metadata and controls
51 lines (45 loc) · 1.36 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
42
43
44
45
46
47
48
49
50
51
/**
* 2832. Maximal Range That Each Element Is Maximum in It
* https://leetcode.com/problems/maximal-range-that-each-element-is-maximum-in-it/
* Difficulty: Medium
*
* You are given a 0-indexed array nums of distinct integers.
*
* Let us define a 0-indexed array ans of the same length as nums in the following way:
* - ans[i] is the maximum length of a subarray nums[l..r], such that the maximum element
* in that subarray is equal to nums[i].
*
* Return the array ans.
*
* Note that a subarray is a contiguous part of the array.
*/
/**
* @param {number[]} nums
* @return {number[]}
*/
var maximumLengthOfRanges = function(nums) {
const n = nums.length;
const leftBounds = new Array(n);
const rightBounds = new Array(n);
const stack = [];
for (let i = 0; i < n; i++) {
while (stack.length > 0 && nums[stack[stack.length - 1]] < nums[i]) {
stack.pop();
}
leftBounds[i] = stack.length > 0 ? stack[stack.length - 1] + 1 : 0;
stack.push(i);
}
stack.length = 0;
for (let i = n - 1; i >= 0; i--) {
while (stack.length > 0 && nums[stack[stack.length - 1]] < nums[i]) {
stack.pop();
}
rightBounds[i] = stack.length > 0 ? stack[stack.length - 1] - 1 : n - 1;
stack.push(i);
}
const result = new Array(n);
for (let i = 0; i < n; i++) {
result[i] = rightBounds[i] - leftBounds[i] + 1;
}
return result;
};