Skip to content

2025-03-13 v. 8.9.4: added "2058. Find the Minimum and Maximum Number of Nodes Between Critical Points" #983

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
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 @@ -727,6 +727,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1996. The Number of Weak Characters in the Game | [Link](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/) | [Link](./lib/medium/1996_the_number_of_weak_characters_in_the_game.rb) | [Link](./test/medium/test_1996_the_number_of_weak_characters_in_the_game.rb) |
| 2007. Find Original Array From Doubled Array | [Link](https://leetcode.com/problems/find-original-array-from-doubled-array/) | [Link](./lib/medium/2007_find_original_array_from_doubled_array.rb) | [Link](./test/medium/test_2007_find_original_array_from_doubled_array.rb) |
| 2023. Number of Pairs of Strings With Concatenation Equal to Target | [Link](https://leetcode.com/problems/number-of-pairs-of-strings-with-concatenation-equal-to-target/) | [Link](./lib/medium/2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb) | [Link](./test/medium/test_2023_number_of_pairs_of_strings_with_concatenation_equal_to_target.rb) |
| 2058. Find the Minimum and Maximum Number of Nodes Between Critical Points | [Link](https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/) | [Link](./lib/medium/2058_find_the_minimum_and_maximum_number_of_nodes_between_critical_points.rb) | [Link](./test/medium/test_2058_find_the_minimum_and_maximum_number_of_nodes_between_critical_points.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) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
| 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) |
Expand Down
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.9.3'
s.version = '8.9.4'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

# https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
# @param {ListNode} head
# @return {Integer[]}
def nodes_between_critical_points(head)
prev = head
p = head.next
index = 2
first_index = -1
prev_index = -1
min = ::Float::INFINITY

while !p.nil? && !p.next.nil?
if (prev.val > p.val && p.next.val > p.val) || (prev.val < p.val && p.next.val < p.val)
first_index = index if first_index == -1
min = [min, index - prev_index].min if prev_index != -1
prev_index = index
end

prev = p
p = p.next
index += 1
end

min == ::Float::INFINITY ? [-1, -1] : [min, prev_index - first_index]
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/common/linked_list'
require_relative '../../lib/medium/2058_find_the_minimum_and_maximum_number_of_nodes_between_critical_points'
require 'minitest/autorun'

class FindTheMinimumAndMaximumNumberOfNodesBetweenCriticalPointsTest < ::Minitest::Test
def test_default_one
assert_equal(
[-1, -1],
nodes_between_critical_points(
::ListNode.from_array(
[3, 1]
)
)
)
end

def test_default_two
assert_equal(
[1, 3],
nodes_between_critical_points(
::ListNode.from_array(
[5, 3, 1, 2, 5, 1, 2]
)
)
)
end

def test_default_three
assert_equal(
[3, 3],
nodes_between_critical_points(
::ListNode.from_array(
[1, 3, 2, 2, 3, 2, 2, 2, 7]
)
)
)
end
end