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.
Two pointer approach: It would be a lot faster if the array was sorted to begin with, but the solution still performs much better than a brute force approach using a nested for loop. I ran a script with various implementations of this function in Python (including using a HashMap / dictionary) implementation which came back with the following results:
Two Sum Performance Comparison
Small Arrays (Size: 500, Cases: 10)
Medium Arrays (Size: 2500, Cases: 10)
Large Arrays (Size: 15000, Cases: 5)
Extra Large Arrays (Size: 50000, Cases: 3)
Note: Two Pointer + Sort is the solution I came up with above. Clearly in this case, a Hash Map implementation is the best choice. Even with a list containing 50,000 elements, the HashMap is able to conjure up a solution in less than 5ms on average. The divergence between the HashMap and two pointer solutions begins to grow larger as the sorting method (which for practical purposes has a time complexity of O(n log(n)), has to deal with increasingly large arrays.
The use of the ternary operator to define the base case (return s.toFixed(2) when n is 0) is quite clever. Not the most readable solution, but definitely a clever one!
In retrospect, toFixed would have been the correct method to use as it only deals with decimals, not the number of digits in the number.
My goal for this one was to keep the code clean and modular, without using the filter function.
I didn't think to define templates like this. Clear, clean code. Great!
I'm trying to understand why if the function is passed a window length of 0, and an offset of 1, the output is expected to be an array of n+1 elements, where n is the length of the array. So for window(0,1, [2, 3]), the expected output would be [[], [], []].
Shouldn't the output correspond to the number of elements in the array?
Peak programming right here - normalize descriptive variable names like this in massive codebases!
the only valid answer
This comment is hidden because it contains spoiler information about the solution
I never would've thought of this solution!
It's super interesting to look at, but quite intuitive if you understand the Array.from() method.