K Closest Points to Origin - Leetcode 973 - Heaps (Python)

  Рет қаралды 4,668

Greg Hogg

Greg Hogg

Күн бұрын

Пікірлер: 14
@GregHogg
@GregHogg 6 ай бұрын
Master Data Structures & Algorithms For FREE at AlgoMap.io!
@user-jm6gp2qc8x
@user-jm6gp2qc8x 4 ай бұрын
its so wonderful to realise that this heap DS is helping us to solve a problem that involves k smallest, k largest etc, by not doing a useless sorting all of the elements involved.
@TheMadisonBluesBand
@TheMadisonBluesBand 2 ай бұрын
Very nicely explained how (n log n), while seemingly similar to (n log k) for trivial inputs, is actually much less efficient.
@itachicodes2506
@itachicodes2506 4 ай бұрын
absolute goat at explaining man (:
@ganeshegmful
@ganeshegmful 3 ай бұрын
Great. One observation, if two are more points are within the square shaped region (1,1) (-1,-1), then I guess we need to compare the square root of squared sum.
@galdali10
@galdali10 3 ай бұрын
That is a really nice and easy to understand solution! But you can also use quick select to improve to O(n)
@kauegatto
@kauegatto 7 ай бұрын
finally understood this problem, ty a lot!
@GregHogg
@GregHogg 7 ай бұрын
Glad to hear it, no worries!
@kumaranb8702
@kumaranb8702 Жыл бұрын
Nice information 😊
@tm-luk317
@tm-luk317 Жыл бұрын
What is the name of the software he uses as a blackboard?
@GregHogg
@GregHogg 11 ай бұрын
He uses miro
@deeps-n5y
@deeps-n5y 11 ай бұрын
@@GregHogg 😂
@tashrique
@tashrique 2 ай бұрын
# Quick Select Aprroach # O(n), O(1) distance = lambda point: point[0]**2 + point[1]**2 def quickselect(l, r): index = random.randint(l,r) pivot = points[index] pivot_dist = distance(pivot) points[index], points[r] = points[r], points[index] p = l for i in range(l, r): if pivot_dist >= distance(points[i]): points[i], points[p] = points[p], points[i] p += 1 points[p], points[r] = points[r], points[p] return p L, R = 0, len(points) - 1 pivot = len(points) while pivot != k: pivot = quickselect(L, R) if pivot > k: R = pivot - 1 else: L = pivot + 1 return points[:k]
@miramar-103
@miramar-103 6 ай бұрын
return heapq.nsmallest(k, points, lambda p : math.sqrt(p[0]**2 + p[1]**2))
Merge K Sorted Linked Lists - Leetcode 23 - Heaps (Python)
10:07
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
Enceinte et en Bazard: Les Chroniques du Nettoyage ! 🚽✨
00:21
Two More French
Рет қаралды 42 МЛН
Number of Islands - Leetcode 200 - Graphs (Python)
11:01
Greg Hogg
Рет қаралды 15 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 245 М.
K CLOSEST POINTS TO ORIGIN | LEETCODE 973 | PYTHON SOLUTION
14:10
Cracking FAANG
Рет қаралды 2,5 М.
Pacific Atlantic Water Flow - Leetcode 417 - Graphs (Python)
17:10
Coin Change - Leetcode 322 - Dynamic Programming (Python)
15:27
K Closest Points to Origin | Leetcode #973
9:18
Techdose
Рет қаралды 23 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 691 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Daily Temperatures - Leetcode 739 - Stacks (Python)
12:35
Greg Hogg
Рет қаралды 10 М.
Clone Graph - Leetcode 133 - Graphs (Python)
13:38
Greg Hogg
Рет қаралды 6 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН