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
Agree!
How are those people who voted this solution with the clever mark??
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
这种方法很棒
It just creates a copy of an array, so that actual argument is not mutated. n could be ommited here, without change, making this less confusing.
Love the code. Cannot for the life of me wrap my head around the need for "const result = signature.slice(0, n);" at the beginning. I keep running through it, and seeing the different results with and without it, but cannot see why it does so. Could you clarify?
const arrLen = result.length; // one time length calculation
while(arrLen < n) {} // Don't need to calculate array length, just check for condition
while(result.length < n) {} // calculate array length on every loop, then check for condition
Loading more items...