From 8e78f828ef0944992025eed1e14966ab9b18c564 Mon Sep 17 00:00:00 2001 From: Deven <63876261+devenperez@users.noreply.github.com> Date: Tue, 17 Jan 2023 23:51:13 -0500 Subject: [PATCH] Time: 31 ms (90.82%), Space: 13.7 MB (99.87%) - LeetHub --- .../1512-number-of-good-pairs.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/1512-number-of-good-pairs/1512-number-of-good-pairs.py b/1512-number-of-good-pairs/1512-number-of-good-pairs.py index 2bc84df..16c89e3 100644 --- a/1512-number-of-good-pairs/1512-number-of-good-pairs.py +++ b/1512-number-of-good-pairs/1512-number-of-good-pairs.py @@ -2,9 +2,27 @@ class Solution: def numIdenticalPairs(self, nums: List[int]) -> int: count = 0 + while len(nums) > 0: + oldLength = len(nums) + + # Removes all instances of nums[0] + nums = [ n for n in nums if n != nums[0] ] + + numOccurances = oldLength - len(nums) + + if numOccurances > 1: + count += int((numOccurances * (numOccurances - 1)) / 2) # numOccurances choose 2 + + return count + + """ + ## Straight-forward solution + count = 0 + for i in range(len(nums)): for j in range(i + 1, len(nums)): if nums[i] == nums[j]: count += 1 - return count \ No newline at end of file + return count + """ \ No newline at end of file