Add brute force approach

This commit is contained in:
Jessica Kwok 2021-05-07 16:37:58 -07:00
parent 010e5ff96a
commit da897fe6ad

View file

@ -8,16 +8,35 @@
# @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