Skip to content

Commit f4d7ad6

Browse files
committed
Runtime: 354 ms (Top 65.08%) | Memory: 83 MB (Top 6.35%)
1 parent 14ca610 commit f4d7ad6

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
1+
// Runtime: 354 ms (Top 65.08%) | Memory: 83 MB (Top 6.35%)
12
var visibleToRight = function(heights){
23
const result = new Array(heights.length).fill(0)
34
const stack = []
45

56
let i = heights.length-1
6-
7+
78
// loop from right to left
8-
while(i >= 0){
9+
while(i >= 0){
910
let popCount = 0
10-
11+
1112
// Keep Popping untill top ele is smaller
1213
while(stack.length > 0 && stack[stack.length-1][0] < heights[i]){
13-
stack.pop()
14+
stack.pop()
1415
popCount+=1
1516
}
16-
17+
1718
/////////////////////
1819
/// After Popping ///
1920
////////////////////
20-
21+
2122
// Case1: if ALL elements got popped
2223
if(stack.length === 0)
2324
result[i] = popCount // mark
24-
25+
2526
// Case2: if NO elements were popped
2627
else if(popCount === 0)
2728
result[i] = 1 // mark
28-
29+
2930
// Case3: if SOME elements were popped
3031
else
3132
result[i] = popCount+1 // mark
32-
33+
3334
// store
3435
stack.push([heights[i],popCount])
35-
36+
3637
i-=1
3738
}
38-
39+
3940
return result
4041
}
4142
var canSeePersonsCount = function(heights) {
4243
return visibleToRight(heights)
4344
};
44-

0 commit comments

Comments
 (0)