diff --git a/medium/tuple_with_same_product.py b/medium/tuple_with_same_product.py new file mode 100644 index 0000000..3c72e38 --- /dev/null +++ b/medium/tuple_with_same_product.py @@ -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