Created
September 2, 2019 22:37
-
-
Save wenweixu/cedfea01507236564b7a21028f38f528 to your computer and use it in GitHub Desktop.
Triple Sum Hackerrank Python solution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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