mirror of
https://github.com/TheAlgorithms/Ruby
synced 2024-12-27 21:58:57 +01:00
Add brute force approach
This commit is contained in:
parent
010e5ff96a
commit
da897fe6ad
1 changed files with 19 additions and 0 deletions
|
@ -8,16 +8,35 @@
|
||||||
# @return {Integer}
|
# @return {Integer}
|
||||||
#
|
#
|
||||||
|
|
||||||
|
#
|
||||||
|
# Approach 1: Brute Force
|
||||||
|
#
|
||||||
|
# Time Complexity: O(n^2)
|
||||||
|
#
|
||||||
def num_identical_pairs(nums)
|
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
|
||||||
|
end
|
||||||
|
count
|
||||||
|
end
|
||||||
|
|
||||||
nums = [1, 2, 3, 1, 1, 3]
|
nums = [1, 2, 3, 1, 1, 3]
|
||||||
puts(num_identical_pairs(nums))
|
puts(num_identical_pairs(nums))
|
||||||
# Output: 4
|
# Output: 4
|
||||||
# Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
|
# Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
|
||||||
|
|
||||||
nums = [1, 1, 1, 1]
|
nums = [1, 1, 1, 1]
|
||||||
puts(num_identical_pairs(nums))
|
puts(num_identical_pairs(nums))
|
||||||
# Output: 6
|
# Output: 6
|
||||||
# Explanation: Each pair in the array are good.
|
# Explanation: Each pair in the array are good.
|
||||||
|
|
||||||
nums = [1, 2, 3]
|
nums = [1, 2, 3]
|
||||||
puts(num_identical_pairs(nums))
|
puts(num_identical_pairs(nums))
|
||||||
# Output: 0
|
# Output: 0
|
Loading…
Reference in a new issue