Minimum Fuel Cost to Report to the Capital - Leetcode 2477 - Python

  Рет қаралды 12,042

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 29
@jamesnguyen3459
@jamesnguyen3459 Жыл бұрын
Stuck with this for today challenge. Thank you for a detail explanation
@keshavgoswami9846
@keshavgoswami9846 8 ай бұрын
I solved the question without seeing any solution and with your idea until 6:10 of the video. Thanks for the great explanation.
@MP-ny3ep
@MP-ny3ep Жыл бұрын
Thank you , this solution seems so easy when you code it up.
@Tim_desu
@Tim_desu Жыл бұрын
So clear and concise, thx dude
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
c++ code for those who are struggling class Solution { public: long long result=0; int dfs(int node,int parent,unordered_map&adj,int seats) { int passengers=0; for(auto &x:adj[node]) { if(x!=parent) { int p=dfs(x,node,adj,seats); passengers+=p; result+=(int)ceil(p*1.0/seats); } } return passengers+1; } long long minimumFuelCost(vector& roads, int seats) { unordered_mapadj; for(auto &x:roads) { adj[x[0]].push_back(x[1]); adj[x[1]].push_back(x[0]); } dfs(0,-1,adj,seats); return result; } };
@PujaKumari-zl7rw
@PujaKumari-zl7rw Жыл бұрын
Hi, Can you tell me why do we need (p*1.0/seats) above?
@laumatthew71
@laumatthew71 Жыл бұрын
​ @Puja Kumari In case we simply use (p / seats), C++ will perform integral division (divide and round down) and return a rounded down integer (which will lead to a wrong answer). p * 1.0 will result in a double type and further divide it will give us the correct result. Hope this help !
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
@@PujaKumari-zl7rw wo example dekho like usme at node 2 we have passengers=3 to be moved from node 2 to 0 and har car me 2 seat hai we have 3 nodes to be moved so we need 2 cars right so we use ceil(p/seats ) and 1.0 for floating value to convert into int value since ceil deals with floating values if we do direct 3/2 here ans is 1 right so 3*1.0/2 lagane se we get ceil(1.5)=2
@CostaKazistov
@CostaKazistov Жыл бұрын
DFS solution is much nicer for this problem compared to BFS (which is doable but slightly harder to write)
@kewtomrao
@kewtomrao Жыл бұрын
This was a really nice problem
@mohithadiyal6083
@mohithadiyal6083 Жыл бұрын
Just amazing explanation
@krateskim4169
@krateskim4169 Жыл бұрын
Awesome explanation
@devinpadron5
@devinpadron5 9 ай бұрын
7:50 shouldn't space complexity be O(N) since we are building an adjacency list?
@AceAlgo
@AceAlgo Жыл бұрын
Bro, I really wish you can fill all LC questions one day lol.
@Codisrocks
@Codisrocks Жыл бұрын
I originally misread it to think that each car had a different capacity. That would have been an interesting problem.
@kwakukusi4094
@kwakukusi4094 Жыл бұрын
best explanation
@akashdey5637
@akashdey5637 5 ай бұрын
why does the dfs call return the number of current passengers?
@mayurpande2433
@mayurpande2433 Жыл бұрын
What if each city has car with variable seat basically we are given list of seats?
@ary_21
@ary_21 Жыл бұрын
Why a second channel when you could have posted this from your main account and add it to the existing graph playlist making it 1 stop solution for all neetcode graph problems ?
@yang5843
@yang5843 Жыл бұрын
He wants to make non-Leetcode solution related content in the future for his main channel
@NeetCodeIO
@NeetCodeIO Жыл бұрын
I can still add these videos to the playlists on the main channel, I'll prob do that and then link the playlists in the video descriptions
@rafaeldietrich8050
@rafaeldietrich8050 Жыл бұрын
That was fast!
@harshitpant07
@harshitpant07 Жыл бұрын
not cleared about something 1st: we are going from bottom to the root nodes right or from root to bottom 2nd: what's the use of passenger+1 , '+1' didn't understand
@kumarspandanpattanayak3489
@kumarspandanpattanayak3489 Жыл бұрын
we are going from root to bottom and passengers + 1 is needed to include the node itself to the passengers variable
@ratin754
@ratin754 Жыл бұрын
0:12 hmm🧐
@NeetCodeIO
@NeetCodeIO Жыл бұрын
😳
@juliuskingsley6212
@juliuskingsley6212 Жыл бұрын
XD
@anshumansinha5874
@anshumansinha5874 5 ай бұрын
I could not understand this, let's take the example at @7:26 , why is it 4 fuel to reach from node 2 to 0? can we not take a car which has 5 seats and take all the 3 people at node 2 so only 1 fuel from there onwards.
As Far from Land as Possible - Leetcode 1162 - Python
12:16
NeetCodeIO
Рет қаралды 10 М.
Shortest Path with Alternating Colors - Leetcode 1129 - Python
13:43
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 6 МЛН
This Game Is Wild...
00:19
MrBeast
Рет қаралды 154 МЛН
If people acted like cats 🙀😹 LeoNata family #shorts
00:22
LeoNata Family
Рет қаралды 11 МЛН
Flip String to Monotone Increasing - Leetcode 926 - Python
15:05
Coding Unbreakable Encryption in C | One-Time Pad
17:42
HirschDaniel
Рет қаралды 4,2 М.
Maximum Matrix Sum - Leetcode 1975 - Python
16:07
NeetCodeIO
Рет қаралды 522
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 852 М.
Why Didn't He Get the Job? Let's Find Out! // Code Review
27:25
The Cherno
Рет қаралды 148 М.
Weekly Contest 320  |  Minimum Fuel Cost to Report to the Capital
19:51
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 329 М.
Detect Capital - LeetCode Interview Coding Challenge [Java Brains]
25:09
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 6 МЛН