Skip to content

2025-01-14 v. 8.0.6: added "1944. Number of Visible People in a Queue" #883

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
26 changes: 26 additions & 0 deletions lib/hard/1944_number_of_visible_people_in_a_queue.rb
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions test/hard/test_1944_number_of_visible_people_in_a_queue.rb
Original file line number Diff line number Diff line change
@@ -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
Loading