SLIDING WINDOW MEDIAN| LEETCODE 480 | PYTHON TWO-HEAP SOLUTION

  Рет қаралды 3,552

Cracking FAANG

Cracking FAANG

Күн бұрын

Channel Discord Community: / discord
Problem Link: leetcode.com/problems/sliding...
Today we are solving a very tricky problem that involves finding the median of a sliding window. We'll need to use two heaps in order to keep everything in check but it definitely isn't trivial how we want to do this. We'll need to use our brains here to come up with a solution and hence the 30 minute long video.
TIMESTAMPS
00:00 Intro
00:07 Question Prompt
00:50 Basic Example
02:49 Solution Intuition
08:00 Coding
27:28Time/Space Complexity
29:15 Outro

Пікірлер: 12
@atul5196
@atul5196 Ай бұрын
1. The SC seems wrong. Since you're using dict (hash table) to track the left most element that goes out of the sliding window, the SC should be O(N) 2. The expression, len(min_heap)
@ishansharma7991
@ishansharma7991 4 ай бұрын
Hey I think, popping from a heap is not O(1), peaking is O(1). Popping itself is two steps delete -which like peak is O(1) - and then a restructuring of the heap, but this restructuring is log(n). So overall popping is a log(n) operation. Overall however, this is still better than the naiive way, searching and delete and restructure. Searching in a heap would be O(n). After the search it would require O(1) to delete and O(n) to re-build / restructure the heap (with heapify) in the worst case. In the best case here for the restructuring, it would take log(n) if the element is either at the top ( here the underlying operation is swap and bubble down) or the last element of the heap (here just delete and bubble up). Taking obviously the worst case, search O(n), delete O(1) and restructure O(n), overall this would be an O(n) operation.
@crackfaang
@crackfaang 4 ай бұрын
Yes I misspoke here. made this problem while I had a cold. The final overall complexity is still correct
@user-mj5nx8yf9b
@user-mj5nx8yf9b 4 ай бұрын
Can someone explain to me how this code deals with the scenario whereby the total number of elements in the heaps (both max_heap and min_heap) does not exceed the size of the sliding window k? The reason I am asking is because this code removes elements from the heap only when the root of the heap is found in heap_dict with a frequency of more than 0. I was thinking what if there is a scenario whereby the element that needs to be removed is not found in the root (but somewhere down the heap), as a result, based on the code, no elements would be removed. Therefore, when we slide the window, we add a new element into the heaps, resulting in the size of both heaps exceeding the size of the sliding window k.
@user-mj5nx8yf9b
@user-mj5nx8yf9b 4 ай бұрын
I have understood. There will be cases whereby the size of both heaps will be more than k, which is more than the size of the window, but that is okay because as long as the top of the heaps are correct, and that our heaps are balanced, we can find the correct medium. So, time complexity wise, it is not exactly log(k) but more like log(k + (some small random number)). The time complexity will still be dominated by log(k)
@boopboop451
@boopboop451 3 ай бұрын
Thanks again for the most clear explanation of this problem I have found! Really appreciate the effort you put into these videos man! One thing that took me a while to grok when watching the video was on line 36 --- to maintain the invariant we initially set (max_heap has one extra element) its a lot more intuitive to change line 36 to "elif balance > 1" (where max_heap has more than 1 element out of balance). Of course, checking for "balance > 0" works here as well (since balance can only be either 0 or +/- 2).
@aakashtyagi
@aakashtyagi 4 ай бұрын
Popping from the top of the heap is O(logN) operation and not O(1) -- 6:02 and 23:43
@crackfaang
@crackfaang 4 ай бұрын
yea I misspoke. the final complexities are still the correct ones
@PedanticAnswerSeeker
@PedanticAnswerSeeker 3 ай бұрын
Before you write your code, could you give a simulated walkthrough of an example and then code it ? that would help make us understand it better
@crackfaang
@crackfaang 3 ай бұрын
Some questions I do this some questions the examples are too much of a hassle and not worth the trouble unfortunately. Just makes the videos take much longer to make and most people just watch the solution bit so it's lot a worthwhile return on investment
@jaimehaynes428
@jaimehaynes428 3 ай бұрын
😈 *promosm*
Meta Product Architecture/Design Interview - Part 1 - Overview
16:30
Cracking FAANG
Рет қаралды 3,6 М.
КАРМАНЧИК 2 СЕЗОН 7 СЕРИЯ ФИНАЛ
21:37
Inter Production
Рет қаралды 484 М.
Я нашел кто меня пранкует!
00:51
Аришнев
Рет қаралды 2,8 МЛН
Жайдарман | Туған күн 2024 | Алматы
2:22:55
Jaidarman OFFICIAL / JCI
Рет қаралды 1,6 МЛН
ROBOT ROOM CLEANER | LEETCODE # 489 | PYTHON BACKTRACK SOLUTION
19:44
Minimum Window Substring | Sliding Window | LeetCode
18:00
AlgosWithMichael
Рет қаралды 38 М.
480. Sliding Window Median - English Version
15:19
Ricky Cho
Рет қаралды 3,8 М.
SUBARRAY SUM EQUALS K | LEETCODE 560 | PYTHON SOLUTION
16:32
Cracking FAANG
Рет қаралды 2,2 М.
EXPRESSION ADD OPERATORS | LEETCODE # 282 | PYTHON SOLUTION
24:05
Cracking FAANG
Рет қаралды 4,6 М.
MAKING A LARGE ISLAND | LEETCODE # 827 | PYTHON SOLUTION
22:09
Cracking FAANG
Рет қаралды 6 М.
LOWEST COMMON ANCESTOR OF A BINARY TREE III [PYTHON]
16:38
Cracking FAANG
Рет қаралды 8 М.
Secret Wireless charger 😱 #shorts
0:28
Mr DegrEE
Рет қаралды 2,5 МЛН
Как слушать музыку с помощью чека?
0:36
YOTAPHONE 2 - СПУСТЯ 10 ЛЕТ
15:13
ЗЕ МАККЕРС
Рет қаралды 163 М.