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.
- Triple sum
- Discussions
Triple sum
Triple sum
Sort by
recency
|
221 Discussions
|
Please Login in order to post a comment
def triplets(a, b, c): a.sort() c.sort() count = 0 for b_val in b: count_a = sum(1 for x in a if x <= b_val) count_c = sum(1 for y in c if b_val>=y) count += count_a * count_c return count
Why is this partially correct?
Used set to remove duplicates and create order. Put data back into vectors to retrieve indices for upper bound checks.
javascript
My algorithm works as follows:: 1. remove duplicates from all vectors (generate sets, then recreate vectors); note that the vectors are now sorted because std::set is an ordered collection 2. for all elements q in B, find the number of elements r in C such that q <= r using binary search, and store the results in vector. Also, generate a prefix sum of that vector starting from its end. 3. for all elements p in A, find the first element q in B such that p <= q. Simply add all triplets with q' >= q in B, that are now stored in the prefix sum array of B.
JS all pass