Skip to content

Commit b321abb

Browse files
authored
2025-01-14 v. 8.0.1: added "946. Validate Stack Sequences"
2 parents 706938c + 508a862 commit b321abb

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
643643
| 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) |
644644
| 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) |
645645
| 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) |
646+
| 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) |
646647
| 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) |
647648
| 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) |
648649
| 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) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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.0'
8+
s.version = '8.0.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/validate-stack-sequences/
4+
# @param {Integer[]} pushed
5+
# @param {Integer[]} popped
6+
# @return {Boolean}
7+
def validate_stack_sequences(pushed, popped)
8+
stack = []
9+
n = pushed.size
10+
p = 0
11+
12+
pushed.each do |push|
13+
stack << push
14+
15+
while !stack.empty? && p < n && stack.last == popped[p]
16+
stack.delete_at(stack.size - 1)
17+
p += 1
18+
end
19+
end
20+
21+
p == n
22+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/946_validate_stack_sequences'
5+
require 'minitest/autorun'
6+
7+
class ValidateStackSequencesTest < ::Minitest::Test
8+
def test_default_one
9+
assert(
10+
validate_stack_sequences(
11+
[1, 2, 3, 4, 5],
12+
[4, 5, 3, 2, 1]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert(
19+
!validate_stack_sequences(
20+
[1, 2, 3, 4, 5],
21+
[4, 3, 5, 1, 2]
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)