Skip to content

Commit 088c870

Browse files
Merge pull request #24 from theforestvn88/august
1944
2 parents 02abfa8 + 20ed68c commit 088c870

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @param {Integer[]} heights
2+
# @return {Integer[]}
3+
def can_see_persons_count(heights)
4+
ans = Array.new(heights.size, 0)
5+
stack = []
6+
heights.each_with_index { |h, i|
7+
while !stack.empty? && heights[stack.last] < h
8+
# each of previous with height < h will see the i-th
9+
ans[stack.pop] += 1
10+
end
11+
12+
# the last will see the i-th
13+
ans[stack.last] += 1 unless stack.empty?
14+
15+
stack.push(i)
16+
}
17+
18+
ans
19+
end

0 commit comments

Comments
 (0)