Skip to content

Instantly share code, notes, and snippets.

@ibeauregard
Created March 25, 2022 00:51
Show Gist options
  • Save ibeauregard/a4a8bceaf85d63d2a31dce45eeaf2c1e to your computer and use it in GitHub Desktop.
Save ibeauregard/a4a8bceaf85d63d2a31dce45eeaf2c1e to your computer and use it in GitHub Desktop.
Given k integer lists all of equal length, return the number of tuples (index[i] for 0 <= i < k) such that sum(arrays[i][index[i]] for 0 <= i < k) == 0.
from collections import Counter
from itertools import product
class Solution:
def fourSumCount(self, *lists):
k = len(lists)
sum_to_count = Counter(sum(nums) for nums in product(*lists[:k // 2]))
return sum(sum_to_count[-sum(nums)] for nums in product(*lists[k // 2:]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment