We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Count Triplets
- Discussions
Count Triplets
Count Triplets
Sort by
recency
|
817 Discussions
|
Please Login in order to post a comment
First we import defaultdictionary from collections module. Then, use this code for better Understanding: def countTriplets(arr, r): total_pairs = 0 count2={} count3={} for num in arr: if num in count3: total_pairs+=count3[num] if num in count2: count3[num*r]=count3.get(num*r, 0) + count2[num] count2[num*r]=count2.get(num*r, 0) + 1 return total_pairs
Easy to understand Python code
Cleanest python solution:
Can be made more succint using a key transform and one dictionary:
This one was a doozy! Took me way longer then I'd care to admit. JS solution (forgive my nesting 😅):
Why is this failing hidden test case 6 :(
I came up with idea of moving forward and keeping a track of singles and doubles that have happened. This is passing all test cases except test case 6 :(