Ad
  • Custom User Avatar

    That is ok, pfela.
    "Best" is a relative word and your solution seems to have better performance.
    I just thoght in a simple solution, but you are right. I am using array_slice to retrieve something I could put in a cache and improve performance.

  • Default User Avatar

    Sorry but this is not best practices. Sure it looks pretty with it being only a few lines of code but it's O(n^2) whereas my solution is O(n).

    Let n = 10000;
    Runtime for this solution ~= 0.0433 seconds
    Runtime for my solution ~= 0.0017 seconds

    Let n = 100000;
    Runtime for this solution ~= 4.8142 seconds
    Runtime for my solution ~= 0.0169 seconds

    Let n = 1000000;
    Runtime for this solution == Fails (out of memory after running locally for over 15 minutes)
    Runtime for my solution ~= 0.1612 seconds

  • Custom User Avatar

    super clever! but looking you code I have to point out that with return array_slice($signature, 0, $n); you dont have to subtrac -3 at the for loop , array slice will cut those extra 3 opperation for you

  • Default User Avatar

    Really a clever solution! :)

  • Default User Avatar