Skip to content

2025-04-23 v. 9.3.2: added "2966. Divide Array Into Arrays With Max Difference" #1022

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Apr 23, 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 @@ -455,6 +455,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 2549. Count Distinct Numbers on Board | [Link](https://leetcode.com/problems/count-distinct-numbers-on-board/) | [Link](./lib/easy/2549_count_distinct_numbers_on_board.rb) | [Link](./test/easy/test_2549_count_distinct_numbers_on_board.rb) |
| 2652. Sum Multiples | [Link](https://leetcode.com/problems/sum-multiples/) | [Link](./lib/easy/2652_sum_multiples.rb) | [Link](./test/easy/test_2652_sum_multiples.rb) |
| 2951. Find the Peaks | [Link](https://leetcode.com/problems/find-the-peaks/) | [Link](./lib/easy/2951_find_the_peaks.rb) | [Link](./test/easy/test_2951_find_the_peaks.rb) |
| 2966. Divide Array Into Arrays With Max Difference | [Link](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/) | [Link](./lib/easy/2966_divide_array_into_arrays_with_max_difference.rb) | [Link](./test/easy/test_2966_divide_array_into_arrays_with_max_difference.rb) |
| 2974. Minimum Number Game | [Link](https://leetcode.com/problems/minimum-number-game/) | [Link](./lib/easy/2974_minimum_number_game.rb) | [Link](./test/easy/test_2974_minimum_number_game.rb) |
| 3083. Existence of a Substring in a String and Its Reverse | [Link](https://leetcode.com/problems/existence-of-a-substring-in-a-string-and-its-reverse/) | [Link](./lib/easy/3083_existence_of_a_substring_in_a_string_and_its_reverse.rb) | [Link](./test/easy/test_3083_existence_of_a_substring_in_a_string_and_its_reverse.rb) |
| 3090. Maximum Length Substring With Two Occurrences | [Link](https://leetcode.com/problems/maximum-length-substring-with-two-occurrences/) | [Link](./lib/easy/3090_maximum_length_substring_with_two_occurrences.rb) | [Link](./test/easy/test_3090_maximum_length_substring_with_two_occurrences.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 = '9.3.1'
s.version = '9.3.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
18 changes: 18 additions & 0 deletions lib/easy/2966_divide_array_into_arrays_with_max_difference.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/
# @param {Integer[]} nums
# @param {Integer} k
# @return {Integer[][]}
def divide_array2966(nums, k)
nums.sort!
result = []

(0...nums.length).step(3) do |i|
return [] if nums[i + 2] - nums[i] > k

result << [nums[i], nums[i + 1], nums[i + 2]]
end

result
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/easy/2966_divide_array_into_arrays_with_max_difference'
require 'minitest/autorun'

class DivideArrayIntoArraysWithMaxDifferenceTest < ::Minitest::Test
def test_default_one
assert_equal(
[
[1, 1, 3],
[3, 4, 5],
[7, 8, 9]
],
divide_array2966(
[1, 3, 4, 8, 7, 9, 3, 5, 1],
3
)
)
end

def test_default_two
assert_equal(
[],
divide_array2966(
[2, 4, 2, 2, 5, 2],
2
)
)
end

def test_default_three
assert_equal(
[
[2, 2, 2],
[4, 5, 5],
[5, 5, 7],
[7, 8, 8],
[9, 9, 10],
[11, 12, 12]
],
divide_array2966(
[4, 2, 9, 8, 2, 12, 7, 12, 10, 5, 8, 5, 5, 7, 9, 2, 5, 11],
14
)
)
end
end