0% completed
Introduction to Sliding Window Pattern
You are given an array and a number K. Find the average of every group of K neighbouring values.
[1, 3, 2, 6, -1, 4, 1, 8, 2] K = 5
The word neighbouring is important. The values in each group must be next to one another. Such a group is called a contiguous subarray.
A simple solution starts at every index. It adds the next K values and divides the sum by K.
This repeats a large amount of work. For every starting position, it performs K additions. The time complexity is O(N * K).
Now compare the first two groups:
[1, 3, 2, 6, -1]
[3, 2, 6, -1, 4]
`
.....
.....
.....
Priyanka Bhosale
· 2 years ago
Is this course still available for me to access after my monthly subscription expires?
aj
· 4 years ago
How is the complexity of brute force O(N*K)? N is the number of elements in input array - which in the example is 9. But the outer loop doesn't run 9 times does it? It will only run from 0 to arr.length - K i.e 4 times.
Adam Woolhether
· 4 years ago
I'll be writing my solutions in Go, anyone interested in reviewing with me?
srilu
· 4 years ago
In the Brute Force Python Solution why is "for i in range(len(arr)-K+1) " used instead of just " for i in range(K)" ? Aren't these two conditions the same thing?
Daniel Lim
· 4 years ago
What is the difference between these two ranges of for loops:
for i in range(len(arr)-K+1)
and
for i in range(K)
joe
· 4 years ago
sum in the O(n * k) element JS solution wasn't initialized properly as 'let sum'
ornella
· 5 years ago
WindowStart is not in the for statement, so it shouldn't iterate, so how it is moving across the array?
ornella
· 5 years ago
Why windowSum is a float?
Reading Progress
0%