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
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 = '7.6.8'
s.version = '7.6.8.1'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
4 changes: 1 addition & 3 deletions lib/easy/136_single_number.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/easy/1512_number_of_good_pairs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions lib/easy/2053_kth_distinct_string_in_an_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/easy/2085_count_common_words_with_one_occurrence.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions lib/easy/350_intersection_of_two_arrays_ii.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/medium/692_top_k_frequent_words.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
Loading