From 75543f2df63c9340455963efa9501eaedb0f5027 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Thu, 6 Feb 2025 22:07:30 +0900 Subject: [PATCH] https://leetcode.com/problems/tuple-with-same-product --- medium/tuple_with_same_product.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 medium/tuple_with_same_product.py 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