Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
A few years late, but for others that are wondering, in vscode just typing /** and hitting enter will create a comment block
You're logging things to the console, aren't you? Don't.
This comment is hidden because it contains spoiler information about the solution
6, the first till is used by 1 and 3 and the second for 2 and 4. 2 + 4 = 6.
The sort is going to be the slowest part. An extra iteration does not make a difference in regards to time complexity.
Regardless, your solution also iterates over the array twice while sorting.
Your solution is also potentially much slower since you put the sort inside the map. Depending on the compiler, your time complexity could be much worse.
I actually wrote them by hand... It doesn't take that long to be honest.
Yeah, changing an array is almost always a bad idea, but
Array.filter()
doesn't do that: it returns a new array.Using a counter would be a great approach, since it never incurs the overhead of creating a new array. On the other hand,
filter
is parallelizable, so it might end up still being faster in practice!One way to square both approaches would be to use
Array.reduce()
to have a counter and avoid changing the original array.Thanks for the suggestion!