Skip to content

Commit 7c8719b

Browse files
authored
2025-03-13 v. 8.9.4: added "2058. Find the Minimum and Maximum Number of Nodes Between Critical Points"
2 parents 7c991b8 + 0d84ba5 commit 7c8719b

4 files changed

+70
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
727727
| 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) |
728728
| 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) |
729729
| 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) |
730+
| 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) |
730731
| 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) |
731732
| 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) |
732733
| 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) |

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.9.3'
8+
s.version = '8.9.4'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/find-the-minimum-and-maximum-number-of-nodes-between-critical-points/
4+
# @param {ListNode} head
5+
# @return {Integer[]}
6+
def nodes_between_critical_points(head)
7+
prev = head
8+
p = head.next
9+
index = 2
10+
first_index = -1
11+
prev_index = -1
12+
min = ::Float::INFINITY
13+
14+
while !p.nil? && !p.next.nil?
15+
if (prev.val > p.val && p.next.val > p.val) || (prev.val < p.val && p.next.val < p.val)
16+
first_index = index if first_index == -1
17+
min = [min, index - prev_index].min if prev_index != -1
18+
prev_index = index
19+
end
20+
21+
prev = p
22+
p = p.next
23+
index += 1
24+
end
25+
26+
min == ::Float::INFINITY ? [-1, -1] : [min, prev_index - first_index]
27+
end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/common/linked_list'
5+
require_relative '../../lib/medium/2058_find_the_minimum_and_maximum_number_of_nodes_between_critical_points'
6+
require 'minitest/autorun'
7+
8+
class FindTheMinimumAndMaximumNumberOfNodesBetweenCriticalPointsTest < ::Minitest::Test
9+
def test_default_one
10+
assert_equal(
11+
[-1, -1],
12+
nodes_between_critical_points(
13+
::ListNode.from_array(
14+
[3, 1]
15+
)
16+
)
17+
)
18+
end
19+
20+
def test_default_two
21+
assert_equal(
22+
[1, 3],
23+
nodes_between_critical_points(
24+
::ListNode.from_array(
25+
[5, 3, 1, 2, 5, 1, 2]
26+
)
27+
)
28+
)
29+
end
30+
31+
def test_default_three
32+
assert_equal(
33+
[3, 3],
34+
nodes_between_critical_points(
35+
::ListNode.from_array(
36+
[1, 3, 2, 2, 3, 2, 2, 2, 7]
37+
)
38+
)
39+
)
40+
end
41+
end

0 commit comments

Comments
 (0)