Ad
  • Custom User Avatar

    Your solution is O(n^2), because you have a for-in and an if-in statement with a list comprehension!

    Because there are tests that involve 10 million elements, even if you had a computer that could iterate through 1 million elements a second, it would take over 13 thousand hours for the entire array to be parsed if there are no matches.

    (10000000*(10000000-1)/2)/(1000000*3600) = 13888.8875
    (3600 is the number of seconds in an hour)

  • Default User Avatar

    good to see Blind4Basics giving his/her normal 'because its more efficient' reply when it clearly isn't.

  • Custom User Avatar

    Yeah, I agree. I started going this way and realized it was probably complicated enough to justify more verbosity. I wouldn't say this is best practice. Certainly clever though.

  • Default User Avatar

    I solved it the same way at first, but in no way is this more efficient.
    At least take the n*2-1 calculation out of the list comprehension. you don't need to calculate that in every loop.

    Check my solutions.

  • Custom User Avatar

    because it's more efficient than a deployed loop