diff --git a/README.md b/README.md index 04062619..becabec5 100644 --- a/README.md +++ b/README.md @@ -643,6 +643,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 919. Complete Binary Tree Inserter | [Link](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Link](./lib/medium/919_complete_binary_tree_inserter.rb) | [Link](./test/medium/test_919_complete_binary_tree_inserter.rb) | | 921. Minimum Add to Make Parentheses Valid | [Link](https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/) | [Link](./lib/medium/921_minimum_add_to_make_parentheses_valid.rb) | [Link](./test/medium/test_921_minimum_add_to_make_parentheses_valid.rb) | | 937. Reorder Data in Log Files | [Link](https://leetcode.com/problems/reorder-data-in-log-files/) | [Link](./lib/medium/937_reorder_data_in_log_files.rb) | [Link](./test/medium/test_937_reorder_data_in_log_files.rb) | +| 946. Validate Stack Sequences | [Link](https://leetcode.com/problems/validate-stack-sequences/) | [Link](./lib/medium/946_validate_stack_sequences.rb) | [Link](./test/medium/test_946_validate_stack_sequences.rb) | | 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) | | 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) | | 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index ec48b6fb..346aa27e 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.0' + s.version = '8.0.1' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/946_validate_stack_sequences.rb b/lib/medium/946_validate_stack_sequences.rb new file mode 100644 index 00000000..5cc78cc5 --- /dev/null +++ b/lib/medium/946_validate_stack_sequences.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/validate-stack-sequences/ +# @param {Integer[]} pushed +# @param {Integer[]} popped +# @return {Boolean} +def validate_stack_sequences(pushed, popped) + stack = [] + n = pushed.size + p = 0 + + pushed.each do |push| + stack << push + + while !stack.empty? && p < n && stack.last == popped[p] + stack.delete_at(stack.size - 1) + p += 1 + end + end + + p == n +end diff --git a/test/medium/test_946_validate_stack_sequences.rb b/test/medium/test_946_validate_stack_sequences.rb new file mode 100644 index 00000000..7b337c5b --- /dev/null +++ b/test/medium/test_946_validate_stack_sequences.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/946_validate_stack_sequences' +require 'minitest/autorun' + +class ValidateStackSequencesTest < ::Minitest::Test + def test_default_one + assert( + validate_stack_sequences( + [1, 2, 3, 4, 5], + [4, 5, 3, 2, 1] + ) + ) + end + + def test_default_two + assert( + !validate_stack_sequences( + [1, 2, 3, 4, 5], + [4, 3, 5, 1, 2] + ) + ) + end +end