Skip to content

Commit dc1c285

Browse files
authored
2025-02-26 v. 8.7.3: added "1630. Arithmetic Subarrays"
2 parents a9d52d1 + 476bfdd commit dc1c285

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
706706
| 1530. Number of Good Leaf Nodes Pairs | [Link](https://leetcode.com/problems/number-of-good-leaf-nodes-pairs/) | [Link](./lib/medium/1530_number_of_good_leaf_nodes_pairs.rb) | [Link](./test/medium/test_1530_number_of_good_leaf_nodes_pairs.rb) |
707707
| 1557. Minimum Number of Vertices to Reach All Nodes | [Link](https://leetcode.com/problems/minimum-number-of-vertices-to-reach-all-nodes/) | [Link](./lib/medium/1557_minimum_number_of_vertices_to_reach_all_nodes.rb) | [Link](./test/medium/test_1557_minimum_number_of_vertices_to_reach_all_nodes.rb) |
708708
| 1609. Even Odd Tree | [Link](https://leetcode.com/problems/even-odd-tree/) | [Link](./lib/medium/1609_even_odd_tree.rb) | [Link](./test/medium/test_1609_even_odd_tree.rb) |
709+
| 1630. Arithmetic Subarrays | [Link](https://leetcode.com/problems/arithmetic-subarrays/) | [Link](./lib/medium/1630_arithmetic_subarrays.rb) | [Link](./test/medium/test_1630_arithmetic_subarrays.rb) |
709710
| 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) |
710711
| 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) |
711712
| 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.7.2'
8+
s.version = '8.7.3'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/arithmetic-subarrays/
4+
# @param {Integer[]} nums
5+
# @param {Integer[]} l
6+
# @param {Integer[]} r
7+
# @return {Boolean[]}
8+
def check_arithmetic_subarrays(nums, l, r)
9+
(0...l.size).map { |i| calculate_for_check_arithmetic_subarrays(nums, l[i], r[i]) }
10+
end
11+
12+
private
13+
14+
# @param {Integer[]} nums
15+
# @param {Integer} l
16+
# @param {Integer} r
17+
# @return {Boolean}
18+
def calculate_for_check_arithmetic_subarrays(nums, l, r)
19+
n = (l...(r + 1)).map { |i| nums[i] }.sort
20+
21+
(0...(n.size - 1)).each do |i|
22+
return false unless n[i + 1] - n[i] == n[1] - n[0]
23+
end
24+
25+
true
26+
end
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1630_arithmetic_subarrays'
5+
require 'minitest/autorun'
6+
7+
class ArithmeticSubarraysTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[
11+
true,
12+
false,
13+
true
14+
],
15+
check_arithmetic_subarrays(
16+
[4, 6, 5, 9, 3, 7],
17+
[0, 0, 2],
18+
[2, 3, 5]
19+
)
20+
)
21+
end
22+
23+
def test_default_two
24+
assert_equal(
25+
[
26+
false,
27+
true,
28+
false,
29+
false,
30+
true,
31+
true
32+
],
33+
check_arithmetic_subarrays(
34+
[-12, -9, -3, -12, -6, 15, 20, -25, -20, -15, -10],
35+
[0, 1, 6, 4, 8, 7],
36+
[4, 4, 9, 7, 9, 10]
37+
)
38+
)
39+
end
40+
end

0 commit comments

Comments
 (0)