Skip to content

Commit 58ccf02

Browse files
authored
2025-04-02 v. 9.1.7: added "2295. Replace Elements in an Array"
2 parents 5d81f9a + 00e11f9 commit 58ccf02

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
751751
| 2233. Maximum Product After K Increments | [Link](https://leetcode.com/problems/maximum-product-after-k-increments/) | [Link](./lib/medium/2233_maximum_product_after_k_increments.rb) | [Link](./test/medium/test_2233_maximum_product_after_k_increments.rb) |
752752
| 2265. Count Nodes Equal to Average of Subtree | [Link](https://leetcode.com/problems/count-nodes-equal-to-average-of-subtree/) | [Link](./lib/medium/2265_count_nodes_equal_to_average_of_subtree.rb) | [Link](./test/medium/test_2265_count_nodes_equal_to_average_of_subtree.rb) |
753753
| 2266. Count Number of Texts | [Link](https://leetcode.com/problems/count-number-of-texts/) | [Link](./lib/medium/2266_count_number_of_texts.rb) | [Link](./test/medium/test_2266_count_number_of_texts.rb) |
754+
| 2295. Replace Elements in an Array | [Link](https://leetcode.com/problems/replace-elements-in-an-array/) | [Link](./lib/medium/2295_replace_elements_in_an_array.rb) | [Link](./test/medium/test_2295_replace_elements_in_an_array.rb) |
754755
| 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) |
755756
| 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) |
756757
| 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

+1-1
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.1.6'
8+
s.version = '9.1.7'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/replace-elements-in-an-array/
4+
# @param {Integer[]} nums
5+
# @param {Integer[][]} operations
6+
# @return {Integer[]}
7+
def array_change(nums, operations)
8+
indices = {}
9+
(0...nums.size).each { |i| indices[nums[i]] = i }
10+
11+
operations.each do |operation|
12+
index = indices[operation[0]]
13+
nums[index] = operation[1]
14+
indices.delete(operation[0])
15+
indices[operation[1]] = index
16+
end
17+
18+
nums
19+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/2295_replace_elements_in_an_array'
5+
require 'minitest/autorun'
6+
7+
class ReplaceElementsInAnArrayTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
[3, 2, 7, 1],
11+
array_change(
12+
[1, 2, 4, 6],
13+
[
14+
[1, 3],
15+
[4, 7],
16+
[6, 1]
17+
]
18+
)
19+
)
20+
end
21+
22+
def test_default_two
23+
assert_equal(
24+
[2, 1],
25+
array_change(
26+
[1, 2],
27+
[
28+
[1, 3],
29+
[2, 1],
30+
[3, 2]
31+
]
32+
)
33+
)
34+
end
35+
end

0 commit comments

Comments
 (0)