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.
it's what I was thinking, basically are 2 nested loops, idiomatically is great but not forp performance.
This is an amazing solution that also scales the best.
Time complexity by using slice and reduce instead of traditional for loop is quite costly i guess
The use of slice and reduce to get the sum is very clever.
array indexing starts at 0 as the first item and goes up to array.length - 1, thus arr[array.length] is add a new item after the last one
Contrary to vincaslt's comment, I believe n is necessary in order to catch the condition vectorjohn points out.
I wonder what the time difference is though. Its probably the same, depending on push() implementation. Using the length to determine the next index should be O(1), right? I guess if they're the same time complexity, push is a LITTLE clearer. I still love the solution though.
I really like the
const result = signature.slice(0, n);
instead of adding slice on the result. I think it's cleaner from the start.In this case it not only copies the array so it isn't modified (a best practice) but also handles when n is less than 3. Without it he'd return the whole signature.
This comment is hidden because it contains spoiler information about the solution
这种方法很棒