"Most applications" is a very subjective phrase. I agree many premature optimizations are evil, but why waste time when the optimization is more like a logical solution than a weird tweak to match certain environment? FYI this solution takes around 500ms for 1 million. Not a huge number in the computer world I would say.
I don't think there's any way to do this in constant time because you're asked to generate a list of m numbers up to an input n. You could absolutely do better than the filter/reduce combo, but I think this gets points for readability.
At present, this has mn time, where n is the input and m is the number of items that match the criteria of being divisible by both 3 and 5. For most applications, the optimization to n time isn't worth it.
This solution is clear and concise, but it filters the entire array twice which is not optimal for large arrays.
What about memory usage? You can not create additional arrays.
Can be solved by searching through array only once. Why two filters solution is Best Practice?
This comment is hidden because it contains spoiler information about the solution
This comment is hidden because it contains spoiler information about the solution
"Most applications" is a very subjective phrase. I agree many premature optimizations are evil, but why waste time when the optimization is more like a logical solution than a weird tweak to match certain environment? FYI this solution takes around 500ms for 1 million. Not a huge number in the computer world I would say.
I don't think there's any way to do this in constant time because you're asked to generate a list of m numbers up to an input n. You could absolutely do better than the
filter
/reduce
combo, but I think this gets points for readability.At present, this has
mn
time, wheren
is the input andm
is the number of items that match the criteria of being divisible by both 3 and 5. For most applications, the optimization ton
time isn't worth it.why take O(n) when you can do it in constant time?
This comment is hidden because it contains spoiler information about the solution