Skip to content

Commit c5c240e

Browse files
committed
2025-01-03 v. 7.6.8.1: small refactoring
1 parent 8989189 commit c5c240e

7 files changed

+9
-12
lines changed

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 = '7.6.8'
8+
s.version = '7.6.8.1'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'

lib/easy/136_single_number.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,5 @@
44
# @param {Integer[]} nums
55
# @return {Integer}
66
def single_number(nums)
7-
count = nums.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 }
8-
9-
count.find { |_key, value| value == 1 }&.first
7+
nums.tally.find { |_key, value| value == 1 }&.first
108
end

lib/easy/1512_number_of_good_pairs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# @return {Integer}
66
def num_identical_pairs(nums)
77
nums
8-
.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 }
8+
.tally
99
.values
1010
.reduce(0) { |acc, elem| acc + elem * (elem - 1) / 2 }
1111
end

lib/easy/2053_kth_distinct_string_in_an_array.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
# @param {Integer} k
66
# @return {String}
77
def kth_distinct(arr, k)
8-
values = arr.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 }
9-
values.each do |key, value|
8+
arr.tally.each do |key, value|
109
next unless value == 1
1110

1211
return key if k == 1

lib/easy/2085_count_common_words_with_one_occurrence.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
# @param {String[]} words2
66
# @return {Integer}
77
def count_words(words1, words2)
8-
w1 = words1.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1; }
9-
w2 = words2.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1; }
8+
w1 = words1.tally
9+
w2 = words2.tally
1010

1111
w1.entries.sum { |entry| entry.last == 1 && w2[entry.first] == 1 ? 1 : 0 }
1212
end

lib/easy/350_intersection_of_two_arrays_ii.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
# @param {Integer[]} nums2
66
# @return {Integer[]}
77
def intersect(nums1, nums2)
8-
nums1_c = nums1.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 }
8+
nums1_c = nums1.tally
99

1010
result = []
1111
nums2.each do |num|
12-
next if nums1_c[num].zero?
12+
next if !nums1_c[num] || nums1_c[num].zero?
1313

1414
result << num
1515
nums1_c[num] -= 1

lib/medium/692_top_k_frequent_words.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# @param {Integer} k
66
# @return {String[]}
77
def top_k_frequent_words(words, k)
8-
values = words.each_with_object(::Hash.new(0)) { |e, total| total[e] += 1; }
8+
values = words.tally
99

1010
to_sort = values.to_a
1111
to_sort.sort! do |a, b|

0 commit comments

Comments
 (0)