Skip to content
Merged

1944 #24

Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions 1901-2000/1944_number_of_visible_people_in_a_queue.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @param {Integer[]} heights
# @return {Integer[]}
def can_see_persons_count(heights)
ans = Array.new(heights.size, 0)
stack = []
heights.each_with_index { |h, i|
while !stack.empty? && heights[stack.last] < h
# each of previous with height < h will see the i-th
ans[stack.pop] += 1
end

# the last will see the i-th
ans[stack.last] += 1 unless stack.empty?

stack.push(i)
}

ans
end