diff --git a/README.md b/README.md index 552c57f8..327bdfdc 100644 --- a/README.md +++ b/README.md @@ -675,3 +675,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 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) | | 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) | | 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) | +| 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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index a6a7d75b..fc1cb6cf 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '8.0.5' + s.version = '8.0.6' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/hard/1944_number_of_visible_people_in_a_queue.rb b/lib/hard/1944_number_of_visible_people_in_a_queue.rb new file mode 100644 index 00000000..69228f28 --- /dev/null +++ b/lib/hard/1944_number_of_visible_people_in_a_queue.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: false + +# https://leetcode.com/problems/number-of-visible-people-in-a-queue/ +# @param {Integer[]} heights +# @return {Integer[]} +def can_see_persons_count(heights) + length = heights.length + stack = [] + result = ::Array.new(length, 0) + + (length - 1).downto(0) do |i| + count = 0 + curr = heights[i] + + while !stack.empty? && stack.last < curr + count += 1 + stack.pop + end + + result[i] = count + (stack.empty? ? 0 : 1) + + stack << curr + end + + result +end diff --git a/test/hard/test_1944_number_of_visible_people_in_a_queue.rb b/test/hard/test_1944_number_of_visible_people_in_a_queue.rb new file mode 100644 index 00000000..3f8907fe --- /dev/null +++ b/test/hard/test_1944_number_of_visible_people_in_a_queue.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: false + +require_relative '../test_helper' +require_relative '../../lib/hard/1944_number_of_visible_people_in_a_queue' +require 'minitest/autorun' + +class NumberOfVisiblePeopleInAQueueTest < ::Minitest::Test + def test_default_one + assert_equal( + [3, 1, 2, 1, 1, 0], + can_see_persons_count( + [10, 6, 8, 5, 11, 9] + ) + ) + end + + def test_default_two + assert_equal( + [4, 1, 1, 1, 0], + can_see_persons_count( + [5, 1, 2, 3, 10] + ) + ) + end +end