Skip to content

Instantly share code, notes, and snippets.

@wenweixu
Created September 2, 2019 22:37
Show Gist options
  • Save wenweixu/cedfea01507236564b7a21028f38f528 to your computer and use it in GitHub Desktop.
Save wenweixu/cedfea01507236564b7a21028f38f528 to your computer and use it in GitHub Desktop.
Triple Sum Hackerrank Python solution
def triplets(a, b, c):
# use set to get unique values, and sort the list
a = sorted(list(set(a)))
b = sorted(list(set(b)))
c = sorted(list(set(c)))
#initiate the number of options in a and c
ia, ic = 0, 0
result = 0
for ib in range(len(b)):
while ia < len(a):
if a[ia] <= b[ib]:
ia += 1
else:
break
while ic < len(c):
if c[ic] <= b[ib]:
ic += 1
else:
break
result += ia * ic
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment