Skip to content

Commit af03a96

Browse files
authored
Implement canSeePersonsCount function for visibility count
1 parent 5c64b01 commit af03a96

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

‎leetcode3/정진영/1944. Number of Visible People in a Queue‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,33 @@ var canSeePersonsCount = function(heights) {
2727

2828
return see;
2929
};
30+
31+
32+
풀이..
33+
/**
34+
* @param {number[]} heights
35+
* @return {number[]}
36+
*/
37+
var canSeePersonsCount = function(heights) {
38+
let see = new Array(heights.length).fill(0);
39+
let stack = [];
40+
41+
for (let i = 0; i < heights.length; i++) {
42+
43+
while (
44+
stack.length > 0 &&
45+
heights[stack[stack.length - 1]] < heights[i]
46+
) {
47+
let person = stack.pop();
48+
see[person]++;
49+
}
50+
51+
if (stack.length > 0) {
52+
see[stack[stack.length - 1]]++;
53+
}
54+
55+
stack.push(i);
56+
}
57+
58+
return see;
59+
};

0 commit comments

Comments
 (0)