Coding Interview Problem - Car Fleet

  Рет қаралды 915

Knapsack

Knapsack

Күн бұрын

Пікірлер: 7
@sanshubhkukutla4043
@sanshubhkukutla4043 2 ай бұрын
Great explanation thanks!
@noble.things
@noble.things 10 ай бұрын
Thanks so much for the excellent explanation of the problem. It really crystallized it for me. That being said, it seems there might be a bug in the code though. In the case where there are no collisions (i.e. no car catches up to another car), the resulting fleet count should be the total number of cars (since each individual car is a fleet), but based on this code, it will be 1. Disclaimer: I didn't actually run the code, but just from looking at it, "fleet_count" is initialized to zero and incremented only when there's a collision, and once again after exiting the "for" loop. Unless I’m missing something….
@SS-cz3vg
@SS-cz3vg Жыл бұрын
Amazing explanation and so easy to understand.
@danc6673
@danc6673 2 жыл бұрын
Keep it up! May I recommend you discuss the solution to the "Longest Substring with At Least K Repeating Characters" problem (lc #395)? Specifically, the sliding window approach, as the divide-and-conquer approach is trivial
@JameS00989
@JameS00989 2 жыл бұрын
Insane explanation thanks Brother 🎉
@dawitdemissie4611
@dawitdemissie4611 2 жыл бұрын
Super!!
@obedpadilla5264
@obedpadilla5264 Жыл бұрын
I finally managed to make my own implementation in java with my limited programming knowledge haha import java.util.Arrays; public class CarFleet { public static void main(String[] args) { int carDistance = 12; int[] carPositions = {2, 0, 4, 7}; int[] carSpeeds = {4, 1, 2, 1}; System.out.println(countCarFleets(carDistance, carPositions, carSpeeds)); // expected fleets: 2 } public static int countCarFleets(int distance, int[] position, int[] speed) { int countFleets = 0; int[] carPositions = position.clone(); int[] carSpeeds = new int[speed.length]; Arrays.sort(carPositions); for(int i = 0; i < carSpeeds.length; ++i) { int index = Arrays.binarySearch(carPositions, position[i]); carSpeeds[index] = speed[i]; } int timeFront = (distance - carPositions[carPositions.length - 1]) / carSpeeds[carSpeeds.length - 1]; for(int i = carSpeeds.length - 2; i > -1; --i) { int timeBack = (distance - carPositions[i]) / carSpeeds[i]; if(timeBack > timeFront) { ++countFleets; timeFront = timeBack; } } return ++countFleets; } }
Coding Interview Problem - Longest String Chain
21:04
Knapsack
Рет қаралды 339
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН
Миллионер | 3 - серия
36:09
Million Show
Рет қаралды 2 МЛН
Players push long pins through a cardboard box attempting to pop the balloon!
00:31
Car Fleet - Leetcode 853 - Python
16:10
NeetCode
Рет қаралды 200 М.
Coding Interview Problem - RLE Iterator
13:01
Knapsack
Рет қаралды 598
How I Failed the Google Coding Interview (and lessons I learned)
14:24
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
How Senior Programmers ACTUALLY Write Code
13:37
Thriving Technologist
Рет қаралды 1,6 МЛН
Coding a Web Server in 25 Lines - Computerphile
17:49
Computerphile
Рет қаралды 345 М.
Coding Interview Problem - Minimum Path Sum
5:49
Knapsack
Рет қаралды 492
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 78 М.
The Traveling Salesman Problem: When Good Enough Beats Perfect
30:27
FOREVER BUNNY
00:14
Natan por Aí
Рет қаралды 25 МЛН