We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 72f030e commit 13c3d70Copy full SHA for 13c3d70
leetcode/1512_number_of_good_pairs.py
@@ -0,0 +1,34 @@
1
+# https://leetcode.com/problems/number-of-good-pairs/
2
+
3
+from typing import List
4
5
+class Solution:
6
+ def numIdenticalPairs(self, nums: List[int]) -> int:
7
+ counter = 0
8
+ length = len(nums)
9
10
+ for i in range(length):
11
+ for j in range(i+1, length):
12
+ if nums[i] == nums[j]:
13
+ counter += 1
14
+ else:
15
+ continue
16
17
+ return counter
18
19
+ def numIdenticalPairsBetter(self, nums: List[int]) -> int:
20
21
+ seen = dict()
22
23
+ for num in nums:
24
+ if num not in seen:
25
+ seen[num] = 1
26
27
+ seen[num] += 1
28
29
+ for num in seen:
30
+ if seen[num] > 1:
31
+ val = seen[num]
32
+ counter += ((val-1) * val) // 2
33
34
0 commit comments