Skip to content
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 @@ -735,6 +735,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
| 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) |
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.0.0'
s.version = '9.0.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
33 changes: 33 additions & 0 deletions lib/medium/2121_intervals_between_identical_elements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

# https://leetcode.com/problems/intervals-between-identical-elements/
# @param {Integer[]} arr
# @return {Integer[]}
def get_distances(arr)
groups = ::Hash.new { |h, k| h[k] = [] }
arr.each_with_index { |num, idx| groups[num] << idx }
answer = ::Array.new(arr.size, 0)

groups.each_value do |indices|
n = indices.size

next if n == 1

prefix = [0]
sum = 0

indices.each do |x|
sum += x
prefix << sum
end

(0...n).each do |i|
current = indices[i]
left = current * i - prefix[i]
right = (prefix[n] - prefix[i + 1]) - current * (n - i - 1)
answer[current] = left + right
end
end

answer
end
25 changes: 25 additions & 0 deletions test/medium/test_2121_intervals_between_identical_elements.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/2121_intervals_between_identical_elements'
require 'minitest/autorun'

class IntervalsBetweenIdenticalElementsTest < ::Minitest::Test
def test_default_one
assert_equal(
[4, 2, 7, 2, 4, 4, 5],
get_distances(
[2, 1, 3, 1, 2, 3, 3]
)
)
end

def test_default_two
assert_equal(
[5, 0, 3, 4],
get_distances(
[10, 5, 10, 10]
)
)
end
end