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
|
801 Discussions
|
Please Login in order to post a comment
O(n) using unordered_map (which are hash objects) and dynamic programming
if there are hash collisions, then it wont be O(n). but this question is under hashmaps section so i presume they want u to use hash objects to do it
what is wrong with this static long countTriplets(List arr, long r) {
I need help too, I may have not understood the assignement and it is just returning wrong value instead of errors. The solution I worked on is by taking the logarithm of the actual numbers so it is easier to solve the triplets. For example instead of having 1 5 25 I will have 0 1 2. I then create a list with my second loop when I take the unique values length of my array and create the possibilities to check [1, 2, 3], [2, 3, 4] etc...
In the end I just count the numbers of good triplets that I could have and I return this count. It is working for the first test cases but niot for the other one without explanation, in term of memory I know that I am super far from perfection but I want to solve it before improving.
Thanks for your help
I need help with the solution below. The idea is that it keeps two dictionaries n_dict and pair_dict, the first one counting how often n has appeared up until now, and pair_dict counting how often the pair (n//r,n) has appeared until now in that order. The third dictionary (triple_dict) can actually be ignored and just replaced with a counter that upticks, but for completion's sake I thought I'd track the triples as well in a dictionary (it counts the triples ((n//r) //r, n//r, n) that appeared so far in that order).
My problem is that it works everywhere, except specifically for Test Case 6, and I have just no clue why not. It seems odd that it works for everything, except this particular one for some reason, and I don't see what makes it different to the other cases.
The
list_to_dict
in the r == 1 case is just a method that maps a list to a dictionary that counts the frequency of each number. Just ignore it.I know the actual solution at this point. I'm just trying to understand why thid solution particularly doesn't work, where does it fail?