code-reviews

tdantas 2019-10-05T17:19:27.032600Z

hey guys, need some help the problem is quite simple:

Given two unsorted arrays team-a and team-b. They may contain duplicates. 
For each element in team-b count elements less than or equal to it in array team-a
my solution was:
(defn solution [team-a team-b]
    (let [steam-a  (sort team-a)
          search #(java.util.Collections/binarySearch steam-a %1)
          mapper  (comp inc search)]
       (mapv #(Math/abs (mapper %1)) team-b)))
the problem is , for huge inputs 100K items for team-a and 100K items for team-b array, the solution is taking ~ 150s the time limit is only 8s, how can I improve the solution ?

jaihindhreddy 2019-10-07T12:11:07.036Z

If (count (distinct team-a)) is small, you can use frequencies and buildup another array with cumulative sums, and make it bit faster.

tdantas 2019-10-05T17:20:10.033200Z

the bottleneck here is the binarySearch, is taking around 1 ~2 millisecond per call

dominicm 2019-10-05T22:00:44.035200Z

You might want to turn on reflection checks. It might indicate a lot of time figuring it out. Alternatively, use async-clj-profiler to pinpoint it exactly

tdantas 2019-10-05T22:05:24.035900Z

Gonna try this @dominicm . Let you know what I found