Skip to content

Commit 19ed489

Browse files
authored
2025-03-05 v. 8.8.3: added "1814. Count Nice Pairs in an Array"
2 parents d10d7c4 + 9c13541 commit 19ed489

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
716716
| 1721. Swapping Nodes in a Linked List | [Link](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/) | [Link](./lib/medium/1721_swapping_nodes_in_a_linked_list.rb) | [Link](./test/medium/test_1721_swapping_nodes_in_a_linked_list.rb) |
717717
| 1780. Check if Number is a Sum of Powers of Three | [Link](https://leetcode.com/problems/check-if-number-is-a-sum-of-powers-of-three/) | [Link](./lib/medium/1780_check_if_number_is_a_sum_of_powers_of_three.rb) | [Link](./test/medium/test_1780_check_if_number_is_a_sum_of_powers_of_three.rb) |
718718
| 1797. Design Authentication Manager | [Link](https://leetcode.com/problems/design-authentication-manager/) | [Link](./lib/medium/1797_design_authentication_manager.rb) | [Link](./test/medium/test_1797_design_authentication_manager.rb) |
719+
| 1814. Count Nice Pairs in an Array | [Link](https://leetcode.com/problems/count-nice-pairs-in-an-array/) | [Link](./lib/medium/1814_count_nice_pairs_in_an_array.rb) | [Link](./test/medium/test_1814_count_nice_pairs_in_an_array.rb) |
719720
| 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) |
720721
| 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) |
721722
| 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

+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 = '8.8.2'
8+
s.version = '8.8.3'
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,37 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/count-nice-pairs-in-an-array/
4+
# @param {Integer[]} nums
5+
# @return {Integer}
6+
def count_nice_pairs(nums)
7+
(0...nums.size).each { |i| nums[i] -= rev(nums[i]) }
8+
9+
count = {}
10+
result = 0
11+
12+
nums.each do |num|
13+
curr = count.fetch(num, 0)
14+
result += curr
15+
result %= 1_000_000_007
16+
17+
count[num] = curr + 1
18+
end
19+
20+
result
21+
end
22+
23+
private
24+
25+
# @param {Integer} num
26+
# @return {Integer}
27+
def rev(num)
28+
reversed = 0
29+
30+
until num.zero?
31+
digit = num % 10
32+
reversed = reversed * 10 + digit
33+
num /= 10
34+
end
35+
36+
reversed
37+
end
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/1814_count_nice_pairs_in_an_array'
5+
require 'minitest/autorun'
6+
7+
class CountNicePairsInAnArrayTest < ::Minitest::Test
8+
def test_default_one
9+
assert_equal(
10+
2,
11+
count_nice_pairs(
12+
[42, 11, 1, 97]
13+
)
14+
)
15+
end
16+
17+
def test_default_two
18+
assert_equal(
19+
4,
20+
count_nice_pairs(
21+
[13, 10, 35, 24, 76]
22+
)
23+
)
24+
end
25+
end

0 commit comments

Comments
 (0)