Skip to content

Commit 89a1f63

Browse files
authored
2025-01-14 v. 8.0.6: added "1944. Number of Visible People in a Queue"
2 parents 9cb6081 + f5330c8 commit 89a1f63

4 files changed

+53
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -675,3 +675,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
675675
| 1106. Parsing A Boolean Expression | [Link](https://leetcode.com/problems/parsing-a-boolean-expression/) | [Link](./lib/hard/1106_parsing_a_boolean_expression.rb) | [Link](./test/hard/test_1106_parsing_a_boolean_expression.rb) |
676676
| 1220. Count Vowels Permutation | [Link](https://leetcode.com/problems/count-vowels-permutation/) | [Link](./lib/hard/1220_count_vowels_permutation.rb) | [Link](./test/hard/test_1220_count_vowels_permutation.rb) |
677677
| 1770. Maximum Score from Performing Multiplication Operations | [Link](https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/) | [Link](./lib/hard/1770_maximum_score_from_performing_multiplication_operations.rb) | [Link](./test/hard/test_1770_maximum_score_from_performing_multiplication_operations.rb) |
678+
| 1944. Number of Visible People in a Queue | [Link](https://leetcode.com/problems/number-of-visible-people-in-a-queue/) | [Link](./lib/hard/1944_number_of_visible_people_in_a_queue.rb) | [Link](./test/hard/test_1944_number_of_visible_people_in_a_queue.rb) |

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '8.0.5'
8+
s.version = '8.0.6'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: false
2+
3+
# https://leetcode.com/problems/number-of-visible-people-in-a-queue/
4+
# @param {Integer[]} heights
5+
# @return {Integer[]}
6+
def can_see_persons_count(heights)
7+
length = heights.length
8+
stack = []
9+
result = ::Array.new(length, 0)
10+
11+
(length - 1).downto(0) do |i|
12+
count = 0
13+
curr = heights[i]
14+
15+
while !stack.empty? && stack.last < curr
16+
count += 1
17+
stack.pop
18+
end
19+
20+
result[i] = count + (stack.empty? ? 0 : 1)
21+
22+
stack << curr
23+
end
24+
25+
result
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: false
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/hard/1944_number_of_visible_people_in_a_queue'
5+
require 'minitest/autorun'
6+
7+
class NumberOfVisiblePeopleInAQueueTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[3, 1, 2, 1, 1, 0],
11+
can_see_persons_count(
12+
[10, 6, 8, 5, 11, 9]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
[4, 1, 1, 1, 0],
20+
can_see_persons_count(
21+
[5, 1, 2, 3, 10]
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)