K Closest Points to the Origin | Brute Force, Sorting, Heaps, & Reduction

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

Interview Pen

Interview Pen

Жыл бұрын

Visit Our Website: interviewpen.com/?...
Join Our Discord (24/7 help): / discord
Join Our Newsletter - The Blueprint: theblueprint.dev/subscribe
Like & Subscribe: / @interviewpen
This is an example of a full video available on interviewpen.com. Check out our website to find more premium content like this!
Problem Statement:
Given an array of points `points` where points[i] = [xᵢ, yᵢ] represents a point on a [Cartesian plane](en.wikipedia.org/wiki/Cartesi...) and an integer `k`, return the `k` closest points to the origin (0, 0).
Point Distance: The distance between any two points A (point 1) & B (point 2) is given by the [Euclidean distance](en.wikipedia.org/wiki/Euclide...) → sqrt((x₁ - x₂)² + (y₁ - y₂)²)
You may return the `k` closest points in any order.
Table of Contents:
0:00 - Problem Introduction
0:44 - Point-to-Point Distance
3:00 - Visit interviewpen.com
3:19 - Initial Thoughts
04:55 - Reducing to “Find K Smallest Items”
05:41 - Continuing On
06:02 - Picking the K Smallest Distances
06:33 - Implementation: Picking w/ Scans
09:02 - Complexities
09:54 - Implementation: Sorting → Plucking 1st k Items
10:47 - Complexities
11:01 - Using a Special Structure
11:53 - Using a Min Heap
12:43 - Using a Max Heap
13:44 - Implementation: Max Heap → Retain k Items
15:11 - Complexities
16:17 - Wrap Up
Erratum:
0:00 - The point written (0, 5) is (5, 0). This is verbally corrected at 2:41.
Socials:
Twitter: / interviewpen
Twitter (The Blueprint): / theblueprintdev
LinkedIn: / interviewpen
Website: interviewpen.com/?...

Пікірлер: 5
@interviewpen
@interviewpen Жыл бұрын
Thanks for watching! (see our description for an Erratum) Visit interviewpen.com/? for more great Data Structures & Algorithms + System Design content 🧎
@ericmargolis1852
@ericmargolis1852 2 ай бұрын
Really good explanation and walkthrough, thank you!
@interviewpen
@interviewpen Ай бұрын
Thanks!
@mav31415
@mav31415 Жыл бұрын
nit: you can skip the square root and you'd get the same ordering.
@interviewpen
@interviewpen Жыл бұрын
makes sense, as we're just interested in a raw comparable magnitude vs. the actual distance -> if both line up then we solve the same problem indeed
Disjoint-Set Data Structure (Union-Find) | Fast Subset Checking
18:43
K Closest Points to Origin | Leetcode #973
9:18
Techdose
Рет қаралды 22 М.
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 18 МЛН
SPILLED CHOCKY MILK PRANK ON BROTHER 😂 #shorts
00:12
Savage Vlogs
Рет қаралды 22 МЛН
Box jumping challenge, who stepped on the trap? #FunnyFamily #PartyGames
00:31
Family Games Media
Рет қаралды 25 МЛН
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Design a Simple Authentication System | System Design Interview Prep
17:22
The Man Who Revolutionized Computer Science With Math
7:50
Quanta Magazine
Рет қаралды 2,8 МЛН
The BEST Way to Find a Random Point in a Circle | #SoME1 #3b1b
18:35
When is a line nearest the origin?
7:50
Prime Newtons
Рет қаралды 8 М.
Heaps, heapsort, and priority queues - Inside code
19:01
Inside code
Рет қаралды 77 М.
K Closest Points to Origin | LeetCode 973 | C++, Java, Python
34:10
Knowledge Center
Рет қаралды 3,9 М.
A little girl was shy at her first ballet lesson #shorts
00:35
Fabiosa Animated
Рет қаралды 18 МЛН