From ff17a65fd3746d34bbdd2776d1d03bb71c7a1c3f Mon Sep 17 00:00:00 2001 From: fartem Date: Wed, 23 Apr 2025 08:23:38 +0300 Subject: [PATCH] 2025-04-23 v. 9.3.2: added "2966. Divide Array Into Arrays With Max Difference" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...e_array_into_arrays_with_max_difference.rb | 18 +++++++ ...e_array_into_arrays_with_max_difference.rb | 48 +++++++++++++++++++ 4 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 lib/easy/2966_divide_array_into_arrays_with_max_difference.rb create mode 100644 test/easy/test_2966_divide_array_into_arrays_with_max_difference.rb diff --git a/README.md b/README.md index f8c7ecf..2d3641f 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index efbe728..85a05a1 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/easy/2966_divide_array_into_arrays_with_max_difference.rb b/lib/easy/2966_divide_array_into_arrays_with_max_difference.rb new file mode 100644 index 0000000..70233cc --- /dev/null +++ b/lib/easy/2966_divide_array_into_arrays_with_max_difference.rb @@ -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 diff --git a/test/easy/test_2966_divide_array_into_arrays_with_max_difference.rb b/test/easy/test_2966_divide_array_into_arrays_with_max_difference.rb new file mode 100644 index 0000000..39bf5e4 --- /dev/null +++ b/test/easy/test_2966_divide_array_into_arrays_with_max_difference.rb @@ -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