Skip to content

Commit 167065d

Browse files
authored
2025-03-18 v. 9.0.1: added "2121. Intervals Between Identical Elements"
2 parents d7b6780 + c4aaa74 commit 167065d

4 files changed

+60
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,6 +735,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
735735
| 2096. Step-By-Step Directions From a Binary Tree Node to Another | [Link](https://leetcode.com/problems/step-by-step-directions-from-a-binary-tree-node-to-another/) | [Link](./lib/medium/2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb) | [Link](./test/medium/test_2096_step_by_step_directions_from_a_binary_tree_node_to_another.rb) |
736736
| 2109. Adding Spaces to a String | [Link](https://leetcode.com/problems/adding-spaces-to-a-string/) | [Link](./lib/medium/2109_adding_spaces_to_a_string.rb) | [Link](./test/medium/test_2109_adding_spaces_to_a_string.rb) |
737737
| 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) |
738+
| 2121. Intervals Between Identical Elements | [Link](https://leetcode.com/problems/intervals-between-identical-elements/) | [Link](./lib/medium/2121_intervals_between_identical_elements.rb) | [Link](./test/medium/test_2121_intervals_between_identical_elements.rb) |
738739
| 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) |
739740
| 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) |
740741
| 2657. Find the Prefix Common Array of Two Arrays | [Link](https://leetcode.com/problems/find-the-prefix-common-array-of-two-arrays/) | [Link](./lib/medium/2657_find_the_prefix_common_array_of_two_arrays.rb) | [Link](./test/medium/test_2657_find_the_prefix_common_array_of_two_arrays.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 = '9.0.0'
8+
s.version = '9.0.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/intervals-between-identical-elements/
4+
# @param {Integer[]} arr
5+
# @return {Integer[]}
6+
def get_distances(arr)
7+
groups = ::Hash.new { |h, k| h[k] = [] }
8+
arr.each_with_index { |num, idx| groups[num] << idx }
9+
answer = ::Array.new(arr.size, 0)
10+
11+
groups.each_value do |indices|
12+
n = indices.size
13+
14+
next if n == 1
15+
16+
prefix = [0]
17+
sum = 0
18+
19+
indices.each do |x|
20+
sum += x
21+
prefix << sum
22+
end
23+
24+
(0...n).each do |i|
25+
current = indices[i]
26+
left = current * i - prefix[i]
27+
right = (prefix[n] - prefix[i + 1]) - current * (n - i - 1)
28+
answer[current] = left + right
29+
end
30+
end
31+
32+
answer
33+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2121_intervals_between_identical_elements'
5+
require 'minitest/autorun'
6+
7+
class IntervalsBetweenIdenticalElementsTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[4, 2, 7, 2, 4, 4, 5],
11+
get_distances(
12+
[2, 1, 3, 1, 2, 3, 3]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
[5, 0, 3, 4],
20+
get_distances(
21+
[10, 5, 10, 10]
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)