n = len(dic) arr = [b for a, b in dic.items()] cnt = 0 for i inrange(n): for j inrange(i+1, n): for k inrange(j+1, n): cnt += arr[i]*arr[j]*arr[k] return cnt
classSolution(object): defunequalTriplets(self, nums): """ :type nums: List[int] :rtype: int """ dic = Counter(nums) n = len(dic) if n < 3: return0 arr = [b for a, b in dic.items()]
cnt = 0 tot = len(nums) for i inrange(n): for j inrange(i+1, n): cnt += arr[i] * arr[j] * (tot-arr[i]-arr[j])
from random import randint from collections import Counter
classSolution(object): defa(self, nums): dic = Counter(nums) cnt = 0 tot = len(nums) for c in dic: cnt += dic[c] * (tot - dic[c]) return cnt // 2
defb(self, nums): pairs = 0 dic = Counter() # 向前確認是否可 pair for i, a inenumerate(nums): pairs += i - dic[a] dic[a] += 1 return pairs
defmain(): k = 1000 for i inrange(k): nums = [randint(1,k) for i inrange(k)] if Solution().a(nums) != Solution().b(nums): print(Solution().a(nums) == Solution().b(nums), Solution().a(nums), Solution().b(nums))
if __name__ == '__main__': main()
同理,將 2 個數推廣至 3 個數,則程式如下:
程式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution(object): defunequalTriplets(self, nums): """ :type nums: List[int] :rtype: int """ trips = pairs = 0 dic = Counter() for i, a inenumerate(nums): # 再向前確認可 pair 的部分是否存在不可 trips 的狀況 trips += pairs - dic[a] * (i - dic[a]) # 向前確認是否可 pair pairs += i - dic[a] dic[a] += 1 return trips
from random import randint from collections import Counter
classSolution(object): defa(self, nums): n = len(nums) cnt = 0 for i inrange(n): for j inrange(i+1, n): for k inrange(j+1, n): for l inrange(k+1, n): # print(nums[i], nums[j], nums[k], nums[l]) iflen(set([nums[i], nums[j], nums[k], nums[l]])) == 4: cnt += 1 return cnt
defb(self, nums): fours = trips = pairs = 0 dic = Counter() for i, a inenumerate(nums): fours += trips - dic[a] * (i - dic[a]) trips += pairs - dic[a] * (i - dic[a]) pairs += i - dic[a] dic[a] += 1 return fours
defmain(): k = 10 for i inrange(k): # nums = [randint(1,k) for i in range(k)] nums = [j for j inrange(i+4)] # print(nums) if Solution().a(nums) != Solution().b(nums): print(Solution().a(nums) == Solution().b(nums), Solution().a(nums), Solution().b(nums))
from random import randint from collections import Counter
classSolution(object): defa(self, nums): n = len(nums) cnt = 0 for i inrange(n): for j inrange(i+1, n): for k inrange(j+1, n): for l inrange(k+1, n): for m inrange(l+1, n): # print(nums[i], nums[j], nums[k], nums[l], nums[m]) iflen(set([nums[i], nums[j], nums[k], nums[l], nums[m]])) == 5: cnt += 1 return cnt
defb(self, nums): fives = fours = trips = pairs = 0 dic = Counter() for i, a inenumerate(nums): fives += fours - dic[a] * (i - dic[a]) fours += trips - dic[a] * (i - dic[a]) trips += pairs - dic[a] * (i - dic[a]) pairs += i - dic[a] dic[a] += 1 return fives
defmain(): k = 10 for i inrange(k): # nums = [randint(1,k) for i in range(k)] nums = [j for j inrange(i+5)] # print(nums) if Solution().a(nums) != Solution().b(nums): print(Solution().a(nums) == Solution().b(nums), Solution().a(nums), Solution().b(nums))