Skip to content

Commit 4c5514c

Browse files
authored
Update 1944-number-of-visible-people-in-a-queue.js
1 parent bca43de commit 4c5514c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

1944-number-of-visible-people-in-a-queue.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,25 @@ const canSeePersonsCount = function(heights) {
2121

2222
return ans;
2323
};
24+
25+
// another
26+
27+
/**
28+
* @param {number[]} heights
29+
* @return {number[]}
30+
*/
31+
const canSeePersonsCount = function(heights) {
32+
const stack = [], n = heights.length, res = Array(n)
33+
for(let i = n - 1; i >= 0; i--) {
34+
const h = heights[i]
35+
let del = 0
36+
while(stack.length && stack[stack.length - 1] <= h) {
37+
stack.pop()
38+
del++
39+
}
40+
res[i] = stack.length ? del + 1 : del
41+
stack.push(h)
42+
}
43+
44+
return res
45+
};

0 commit comments

Comments
 (0)