From c5c240eb308ee92d45ff73523f8c3ff31f451074 Mon Sep 17 00:00:00 2001 From: fartem Date: Fri, 3 Jan 2025 01:18:22 +0300 Subject: [PATCH] 2025-01-03 v. 7.6.8.1: small refactoring --- leetcode-ruby.gemspec | 2 +- lib/easy/136_single_number.rb | 4 +--- lib/easy/1512_number_of_good_pairs.rb | 2 +- lib/easy/2053_kth_distinct_string_in_an_array.rb | 3 +-- lib/easy/2085_count_common_words_with_one_occurrence.rb | 4 ++-- lib/easy/350_intersection_of_two_arrays_ii.rb | 4 ++-- lib/medium/692_top_k_frequent_words.rb | 2 +- 7 files changed, 9 insertions(+), 12 deletions(-) diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 78883221..15d2744c 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '7.6.8' + s.version = '7.6.8.1' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/easy/136_single_number.rb b/lib/easy/136_single_number.rb index 20f4c14e..ee5e6e64 100644 --- a/lib/easy/136_single_number.rb +++ b/lib/easy/136_single_number.rb @@ -4,7 +4,5 @@ # @param {Integer[]} nums # @return {Integer} def single_number(nums) - count = nums.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 } - - count.find { |_key, value| value == 1 }&.first + nums.tally.find { |_key, value| value == 1 }&.first end diff --git a/lib/easy/1512_number_of_good_pairs.rb b/lib/easy/1512_number_of_good_pairs.rb index 55e18f09..ba8d8806 100644 --- a/lib/easy/1512_number_of_good_pairs.rb +++ b/lib/easy/1512_number_of_good_pairs.rb @@ -5,7 +5,7 @@ # @return {Integer} def num_identical_pairs(nums) nums - .each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 } + .tally .values .reduce(0) { |acc, elem| acc + elem * (elem - 1) / 2 } end diff --git a/lib/easy/2053_kth_distinct_string_in_an_array.rb b/lib/easy/2053_kth_distinct_string_in_an_array.rb index 1f83ad99..4e83a81c 100644 --- a/lib/easy/2053_kth_distinct_string_in_an_array.rb +++ b/lib/easy/2053_kth_distinct_string_in_an_array.rb @@ -5,8 +5,7 @@ # @param {Integer} k # @return {String} def kth_distinct(arr, k) - values = arr.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 } - values.each do |key, value| + arr.tally.each do |key, value| next unless value == 1 return key if k == 1 diff --git a/lib/easy/2085_count_common_words_with_one_occurrence.rb b/lib/easy/2085_count_common_words_with_one_occurrence.rb index 027f2256..4bbd3db1 100644 --- a/lib/easy/2085_count_common_words_with_one_occurrence.rb +++ b/lib/easy/2085_count_common_words_with_one_occurrence.rb @@ -5,8 +5,8 @@ # @param {String[]} words2 # @return {Integer} def count_words(words1, words2) - w1 = words1.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1; } - w2 = words2.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1; } + w1 = words1.tally + w2 = words2.tally w1.entries.sum { |entry| entry.last == 1 && w2[entry.first] == 1 ? 1 : 0 } end diff --git a/lib/easy/350_intersection_of_two_arrays_ii.rb b/lib/easy/350_intersection_of_two_arrays_ii.rb index a1c2ca25..17de990f 100644 --- a/lib/easy/350_intersection_of_two_arrays_ii.rb +++ b/lib/easy/350_intersection_of_two_arrays_ii.rb @@ -5,11 +5,11 @@ # @param {Integer[]} nums2 # @return {Integer[]} def intersect(nums1, nums2) - nums1_c = nums1.each_with_object(::Hash.new(0)) { |elem, acc| acc[elem] += 1 } + nums1_c = nums1.tally result = [] nums2.each do |num| - next if nums1_c[num].zero? + next if !nums1_c[num] || nums1_c[num].zero? result << num nums1_c[num] -= 1 diff --git a/lib/medium/692_top_k_frequent_words.rb b/lib/medium/692_top_k_frequent_words.rb index 90f6b838..d1025d21 100644 --- a/lib/medium/692_top_k_frequent_words.rb +++ b/lib/medium/692_top_k_frequent_words.rb @@ -5,7 +5,7 @@ # @param {Integer} k # @return {String[]} def top_k_frequent_words(words, k) - values = words.each_with_object(::Hash.new(0)) { |e, total| total[e] += 1; } + values = words.tally to_sort = values.to_a to_sort.sort! do |a, b|