Skip to content

Commit 5272426

Browse files
authored
Create 1512_Number_of_Good_Pairs.md
1 parent 26d8af9 commit 5272426

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

1512_Number_of_Good_Pairs.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Given an array of integers nums.
2+
3+
A pair (i,j) is called good if nums[i] == nums[j] and i < j.
4+
5+
Return the number of good pairs.
6+
7+
Example 1:
8+
9+
Input: nums = [1,2,3,1,1,3]
10+
Output: 4
11+
Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed.
12+
Example 2:
13+
14+
Input: nums = [1,1,1,1]
15+
Output: 6
16+
Explanation: Each pair in the array are good.
17+
Example 3:
18+
19+
Input: nums = [1,2,3]
20+
Output: 0
21+
22+
23+
Constraints:
24+
25+
1 <= nums.length <= 100
26+
1 <= nums[i] <= 100
27+
28+
0. create a counter
29+
1. create a hash table: a key is number
30+
2. if key's value > 1 nCr r=2 and add the result to counter
31+
3. return counter
32+
33+
```python
34+
from math import factorial
35+
def calc_pattern(self, n):
36+
return factorial(n) // factorial(2) // factorial(n - 2)
37+
38+
def numIdenticalPairs(self, nums: List[int]) -> int:
39+
from collections import Counter
40+
dic = Counter(nums)
41+
counter = 0
42+
for k,v in dic.items():
43+
if v >1:
44+
counter += self.calc_pattern(v)
45+
return counter
46+
```

0 commit comments

Comments
 (0)