This commit is contained in:
parent
c3e77d1121
commit
75543f2df6
1 changed files with 22 additions and 0 deletions
22
medium/tuple_with_same_product.py
Normal file
22
medium/tuple_with_same_product.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# https://leetcode.com/problems/tuple-with-same-product
|
||||
|
||||
from typing import Dict, List
|
||||
|
||||
class Solution:
|
||||
def tupleSameProduct(self, nums: List[int]) -> int:
|
||||
map: Dict[int, int] = {}
|
||||
for i in range(len(nums)):
|
||||
for j in range(i + 1, len(nums)):
|
||||
map[nums[i] * nums[j]] = map.get(nums[i] * nums[j], 0) + 1
|
||||
def perm(n: int, r: int = 2) -> int:
|
||||
if n < r:
|
||||
return 0
|
||||
result = 1
|
||||
for i in range(n, n - r, -1):
|
||||
result *= i
|
||||
return result
|
||||
|
||||
result = 0
|
||||
for k in map:
|
||||
result += perm(map[k]) * 4
|
||||
return result
|
||||
Loading…
Add table
Add a link
Reference in a new issue