Skip to content

Commit 13c3d70

Browse files
committed
Adding solution
1 parent 72f030e commit 13c3d70

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

leetcode/1512_number_of_good_pairs.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
counter = 0
21+
seen = dict()
22+
23+
for num in nums:
24+
if num not in seen:
25+
seen[num] = 1
26+
else:
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+
return counter

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy