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
|
796 Discussions
|
Please Login in order to post a comment
You can use the sweep order to handle the problem well. Neat question.
Python O(n) Maintain two counters: one contains the frequency of each element at the right of the current element and the other the frequency of each element at the left. Them, iterate over the array, remove one occurence of the current element from right counter, sum to the triplets the frequency of (element / r) in left counter * frequency of (element * r) in right counter and add one occurence of element in left counter.
My C++ solution from 6 years ago. Gets 100%:
long getMax(long a, long b) { if (a>b) return a; return b; }
// Complete the countTriplets function below. long countTriplets(vector arr, long r) { typedef std::unordered_map HashNumberCounts;
}
javascript
this version is more intuitive but fails on case 3. the version down below improved performance based on it.