Skip to content

2025-03-14 v. 8.9.7: added "2091. Removing Minimum and Maximum From Array" #986

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
Mar 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 @@ -730,6 +730,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 2063. Vowels of All Substrings | [Link](https://leetcode.com/problems/vowels-of-all-substrings/) | [Link](./lib/medium/2063_vowels_of_all_substrings.rb) | [Link](./test/medium/test_2063_vowels_of_all_substrings.rb) |
| 2074. Reverse Nodes in Even Length Groups | [Link](https://leetcode.com/problems/reverse-nodes-in-even-length-groups/) | [Link](./lib/medium/2074_reverse_nodes_in_even_length_groups.rb) | [Link](./test/medium/test_2074_reverse_nodes_in_even_length_groups.rb) |
| 2091. Removing Minimum and Maximum From Array | [Link](https://leetcode.com/problems/removing-minimum-and-maximum-from-array/) | [Link](./lib/medium/2091_removing_minimum_and_maximum_from_array.rb) | [Link](./test/medium/test_2091_removing_minimum_and_maximum_from_array.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.6'
s.version = '8.9.7'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
22 changes: 22 additions & 0 deletions lib/medium/2091_removing_minimum_and_maximum_from_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

# https://leetcode.com/problems/removing-minimum-and-maximum-from-array/
# @param {Integer[]} nums
# @return {Integer}
def minimum_deletions(nums)
size = nums.size
min_index = 0
max_index = 0

(1...size).each do |i|
num = nums[i]
max_index = i if num > nums[max_index]
min_index = i if num < nums[min_index]
end

a = [min_index, max_index].max + 1
b = size - [min_index, max_index].min
c = min_index > max_index ? max_index + 1 + size - min_index : min_index + 1 + size - max_index

[a, b, c].min
end
34 changes: 34 additions & 0 deletions test/medium/test_2091_removing_minimum_and_maximum_from_array.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/2091_removing_minimum_and_maximum_from_array'
require 'minitest/autorun'

class RemovingMinimumAndMaximumFromArrayTest < ::Minitest::Test
def test_default_one
assert_equal(
5,
minimum_deletions(
[2, 10, 7, 5, 4, 1, 8, 6]
)
)
end

def test_default_two
assert_equal(
3,
minimum_deletions(
[0, -4, 19, 1, 8, -2, -3, 5]
)
)
end

def test_default_three
assert_equal(
1,
minimum_deletions(
[101]
)
)
end
end