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.
@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
@Tim_desu
@Tim_desu Жыл бұрын
So clear and concise, thx dude
@CostaKazistov
@CostaKazistov Жыл бұрын
DFS solution is much nicer for this problem compared to BFS (which is doable but slightly harder to write)
@mohithadiyal6083
@mohithadiyal6083 Жыл бұрын
Just amazing explanation
@krateskim4169
@krateskim4169 Жыл бұрын
Awesome explanation
@AceAlgo
@AceAlgo Жыл бұрын
Bro, I really wish you can fill all LC questions one day lol.
@kwakukusi4094
@kwakukusi4094 Жыл бұрын
best explanation
@devinpadron5
@devinpadron5 9 ай бұрын
7:50 shouldn't space complexity be O(N) since we are building an adjacency list?
@Codisrocks
@Codisrocks Жыл бұрын
I originally misread it to think that each car had a different capacity. That would have been an interesting problem.
@akashdey5637
@akashdey5637 5 ай бұрын
why does the dfs call return the number of current passengers?
@kewtomrao
@kewtomrao Жыл бұрын
This was a really nice problem
@rafaeldietrich8050
@rafaeldietrich8050 Жыл бұрын
That was fast!
@mayurpande2433
@mayurpande2433 Жыл бұрын
What if each city has car with variable seat basically we are given list of seats?
@ratin754
@ratin754 Жыл бұрын
0:12 hmm🧐
@NeetCodeIO
@NeetCodeIO Жыл бұрын
😳
@juliuskingsley6212
@juliuskingsley6212 Жыл бұрын
XD
@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
@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
@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.
Detonate the Maximum Bombs - Leetcode 2101 - Python
11:20
NeetCodeIO
Рет қаралды 14 М.
How I Failed the Google Coding Interview (and lessons I learned)
14:24
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 43 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3 МЛН
ТВОИ РОДИТЕЛИ И ЧЕЛОВЕК ПАУК 😂#shorts
00:59
BATEK_OFFICIAL
Рет қаралды 6 МЛН
The Singing Challenge #joker #Harriet Quinn
00:35
佐助与鸣人
Рет қаралды 44 МЛН
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 329 М.
Binary Search Tree Episode 06: Node Deletion
45:25
Shieth شِيث
Рет қаралды 6
As Far from Land as Possible - Leetcode 1162 - Python
12:16
NeetCodeIO
Рет қаралды 10 М.
Flip String to Monotone Increasing - Leetcode 926 - Python
15:05
Microservices are Technical Debt
31:59
NeetCodeIO
Рет қаралды 642 М.
Shortest Path with Alternating Colors - Leetcode 1129 - Python
13:43
Time Needed to Inform All Employees - Leetcode 1376 - Python
10:42
Evaluate Division - Leetcode 399 - Python
17:37
NeetCodeIO
Рет қаралды 32 М.
Disrespect or Respect 💔❤️
00:27
Thiago Productions
Рет қаралды 43 МЛН