Code with Jess - Leetcode #239 Sliding Window Maximum

preview_player
Показать описание
Thanks for watching!
Please let me know what other problems do you want me to do :)

The link to the problem:
Рекомендации по теме
Комментарии
Автор

Such a clear train of thought! Thanks for explaining. It was very helpful

abhilashsulibela
Автор

Here is my solution - class Solution {
public int[] maxSlidingWindow(int[] nums, int k) {


if(nums == null || nums.length == 0 || k > nums.length){
return new int[0];
}

// create a max heap priority queue
PriorityQueue<Integer> pq = new PriorityQueue<Integer>(k, Collections.reverseOrder());

int[] result = new int[nums.length - k + 1];

int windowStart = 0;
for (int windowEnd = 0; windowEnd < nums.length; windowEnd++) {
// keep adding the elements from the array till it hit the window size of 'k'
pq.add(nums[windowEnd]);


// slide the window, we don't need to slide if we've not hit the required window size of 'k'
if (windowEnd >= k - 1) {
// Max heap will return maximum number as max value will be at head
result[windowStart] = pq.peek();
// remove the element at start index of the current window from the max heap before sliding to next window
pq.remove(nums[windowStart]);
// slide to the next window
windowStart++;
}
}


return result;
}
}

arif
Автор

Thank you! It's the only video that's helped me actually understand sliding window with deque

hiimnormal
Автор

Nice video, @5:45 you said you are keeping ascending order deque. I think you are keeping values in descending order in deque. so you can find max element at 0 index. Please correct me if I am wrong.

Sohanbadaya
Автор

Wow, Jess, this is amazing! Please make more videos like this!

weizhou
Автор

So all this time I was pronouncing "deQewww" in a wrong way. 😅😅

ahmadalk
Автор

It was very clear, checked other videos with same logic, but yours is pretty neat.

PrathamMantri
Автор

Thank you so much, I was having a lot of trouble trying to understand this problem! You should solve more problems

lugiadark
Автор

If the window is larger than the array, wouldn't we just return the array with the largest element? I think instead the edge case should be if K < 1. Then there's no window, thus returning empty array.

snaily
Автор

if we were to find the minimum out of the result array then what would be the optimal solution. I thought of using a minHeap instead of an array to store the integers as the window slides and then take to top from minHeap at the end. is there an optimal way to solve this?

NikhilYadav-ylhf
Автор

Thanks for the explanation.. It helped to at least wrap my head around the solution

sumitghewade
Автор

Hi Jess, thanks for your videos. Are you planning to make more?

xueli
Автор

Why are we adding the indexes to the deque and not the actual array elements?

rajathanand
Автор

Thanks, great video. Also TIL deque is pronounced like deck.

siobhanahbois
Автор

How did you get idea of deque? I was thinking more like max heap (O(1) lookup time for max element).

sarthakbhatt
Автор

Graphs!Do something on how to begin with graphs

pruthvirajk
Автор

if( !dq.isEmpty() && dq.peekFirst() == i - k) {
dq.pollFirst();
}

what does it really do? Can you please explain. Thanks :)

shashankshekhar
Автор

Attractive and beautiful sound, must be native American ^_^

tankman
Автор

I don't understand line 20 here. if(!dq.isEmpty() && dq.peekFirst() == i - k).... peekFirst() returns the actual deque value, not its index, so why are we comparing it to the index minus the window size (i - k)? I see that it works but I am having trouble following the logic here.

headyshotta
visit shbcf.ru