educative.io

Educative

My first Javascript implementation

Not sure about the complexity of my JS implementation, but it seems a lot more readable to me. What are your thoughts?

const findMaxSlidingWindow = function(input, size) {
  //Write your code
  const output = [];
  const len = input.length - size
  
  function scan (i, size) {
    let nums = []
    for (let k = 0; k < size; k++) {
      nums.push(input[i+k])
    }
    return nums
  }

  let i = 0
  while(i <= len) {
    output.push(Math.max(...scan(i, size)))
    i++
  }

  console.log('window maximums', output)

  return output;
};

@Adam_Ziolkowski Yes, your code is working well, but its time complexity is n^2. I suggest you make it more optimal by reducing its time complexity.

1 Like