mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-29 20:34:27 +01:00
Merge pull request #148 from jsca-kwok/jk-good-pairs
Good pairs algorithm
This commit is contained in:
commit
584cb7a643
3 changed files with 87 additions and 0 deletions
|
@ -25,6 +25,7 @@
|
|||
* [Find The Highest Altitude](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/find_the_highest_altitude.rb)
|
||||
* [Fizz Buzz](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/fizz_buzz.rb)
|
||||
* [Get Products Of All Other Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/get_products_of_all_other_elements.rb)
|
||||
* [Good Pairs](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/good_pairs.rb)
|
||||
* [Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/intersection.rb)
|
||||
* [Next Greater Element](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/next_greater_element.rb)
|
||||
* [Remove Elements](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/remove_elements.rb)
|
||||
|
@ -51,6 +52,7 @@
|
|||
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/anagram_checker.rb)
|
||||
* [Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/arrays_intersection.rb)
|
||||
* [Find All Duplicates In An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/find_all_duplicates_in_an_array.rb)
|
||||
* [Good Pairs](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/good_pairs.rb)
|
||||
* [Richest Customer Wealth](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/richest_customer_wealth.rb)
|
||||
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/hash_table/two_sum.rb)
|
||||
* Linked Lists
|
||||
|
|
42
data_structures/arrays/good_pairs.rb
Normal file
42
data_structures/arrays/good_pairs.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
# Challenge name: Number of good pairs
|
||||
#
|
||||
# Given an array of integers nums.
|
||||
# A pair (i,j) is called good if nums[i] == nums[j] and i < j.
|
||||
# Return the number of good pairs.
|
||||
#
|
||||
# @param {Integer[]} nums
|
||||
# @return {Integer}
|
||||
#
|
||||
|
||||
#
|
||||
# Approach 1: Brute Force
|
||||
#
|
||||
# Time Complexity: O(n^2)
|
||||
#
|
||||
def num_identical_pairs(nums)
|
||||
count = 0
|
||||
nums.each_with_index do |num, i|
|
||||
target = num
|
||||
nums.each_with_index do |num, j|
|
||||
next if i >= j
|
||||
if num == target
|
||||
count += 1
|
||||
end
|
||||
end
|
||||
end
|
||||
count
|
||||
end
|
||||
|
||||
nums = [1, 2, 3, 1, 1, 3]
|
||||
puts(num_identical_pairs(nums))
|
||||
# Output: 4
|
||||
# Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
|
||||
|
||||
nums = [1, 1, 1, 1]
|
||||
puts(num_identical_pairs(nums))
|
||||
# Output: 6
|
||||
# Explanation: Each pair in the array are good.
|
||||
|
||||
nums = [1, 2, 3]
|
||||
puts(num_identical_pairs(nums))
|
||||
# Output: 0
|
43
data_structures/hash_table/good_pairs.rb
Normal file
43
data_structures/hash_table/good_pairs.rb
Normal file
|
@ -0,0 +1,43 @@
|
|||
# Challenge name: Number of good pairs
|
||||
#
|
||||
# Given an array of integers nums.
|
||||
# A pair (i,j) is called good if nums[i] == nums[j] and i < j.
|
||||
# Return the number of good pairs.
|
||||
#
|
||||
# @param {Integer[]} nums
|
||||
# @return {Integer}
|
||||
#
|
||||
|
||||
#
|
||||
# Approach 1: Hash
|
||||
#
|
||||
# Time Complexity: O(n)
|
||||
def num_identical_pairs(nums)
|
||||
hash = Hash.new(0)
|
||||
|
||||
nums.each do |num|
|
||||
hash[num] = hash[num] + 1
|
||||
end
|
||||
|
||||
counter = 0
|
||||
# Count how many times each number appears.
|
||||
# If a number appears n times, then n * (n – 1) / 2 good pairs
|
||||
# can be made with this number.
|
||||
hash.values.each do |val|
|
||||
counter += (val * (val - 1) / 2)
|
||||
end
|
||||
|
||||
counter
|
||||
end
|
||||
|
||||
nums = [1, 2, 3, 1, 1, 3]
|
||||
puts(num_identical_pairs(nums))
|
||||
# Output: 4
|
||||
|
||||
nums = [1, 1, 1, 1]
|
||||
puts(num_identical_pairs(nums))
|
||||
# Output: 6
|
||||
|
||||
nums = [1, 2, 3]
|
||||
puts(num_identical_pairs(nums))
|
||||
# Output: 0
|
Loading…
Add table
Reference in a new issue