Ad
  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Use it if you want. It's just a tool among others.

    As for what would make it best practices or not... Here, it's certainly not, since it makes the algorithm O(N²) (and yet, it might very well be faster than a proper linear solution, unless N grows very large... x) )

  • Default User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Default User Avatar

    I agree. Replace "/" with "//" and this solution is pretty close to perfect.

  • Default User Avatar

    I wrote a recursive O(n) implementation, but I think this solution is closer to "best practice". Start with this solution, because it is simple and easy to read, understand, and maintain. If you need faster performance, and only if you need faster performance, profile your code and find the bottleneck. Finally, if this part of the code turns out to be the bottleneck, now you can implement a faster algorithm. As Donald Knuth said, "premature optimization is the root of all evil (or at least most of it) in programming".