educative.io

Educative

Seems simpler to use built-in functionality - but is it not the "optimal" solution?

Is there an implied “rule” that we shouldn’t be using built-in objects/methods/etc. to accomplish this (or any other) problem in the course? Or is it that the provided solutions are the most optimal in terms of memory and/or performance? For example, the code I submitted passed all the test cases, but is it not performant?

Course / problem: Educative: Interactive Courses for Software Developers

  let findMaxSlidingWindow = function(nums, windowSize) {
  let result = [];
  let windowValues = [];

  for (let i = 0; i <= nums.length - windowSize; i++) {
    windowValues = nums.slice(i, i + windowSize);
    result[i] = Math.max(...windowValues);
  }

  return result;
};

Hi Cousin,
I have seen your solution, and your solution is alright, but I think your solution needs to be optimal as the “.Slice()” function has O(N) complexity, and the “for loop” has O(N) as well. It becomes O(N2), but when you look at the solution given in the lesson, Its complexity is O(N).
Suggestion:- Try to avoid built functions for optimal solutions.

Thanks, Usama. That makes sense.

1 Like