Тёмный

Gas Station or Circular Tour problem 

Code with Alisha
Подписаться 29 тыс.
Просмотров 14 тыс.
50% 1

There are n gas stations along a circular route, where the amount of gas at the ith station is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from the ith station to its next (i + 1)th station. You begin the journey with an empty tank at one of the gas stations.
Given two integer arrays gas and cost, return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1. If there exists a solution, it is guaranteed to be unique
Example 1:
Input: gas = [1,2,3,4,5], cost = [3,4,5,1,2]
Output: 3
Explanation:
Start at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4
Travel to station 4. Your tank = 4 - 1 + 5 = 8
Travel to station 0. Your tank = 8 - 2 + 1 = 7
Travel to station 1. Your tank = 7 - 3 + 2 = 6
Travel to station 2. Your tank = 6 - 4 + 3 = 5
Travel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.
Therefore, return 3 as the starting index.

Опубликовано:

 

27 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 41   
@habittolearn2423
@habittolearn2423 Год назад
After watching this video, I thought I was being silly for thinking of a solution that was in the complex direction. Thanks for providing such an approachable and simple option.
@putramakatita8814
@putramakatita8814 11 месяцев назад
You are the best on simplify the complex. Great teacher!!
@yadav_utkar8h
@yadav_utkar8h Год назад
Great solution. You made me realize that sometimes we need to think simply rather than overcomplicating the problem.
@Satya-g5t
@Satya-g5t 12 дней назад
Ha ha ha very funny, your Birbal story, it completely made me happy. 😄 , I am still only half way through, stopped the video to comment, will resume now and see the rest of solution.
@TheJack3120
@TheJack3120 Год назад
Thank you for consider case where we can have multiple starting points. I have watched other solution videos on same problem but none of them considered the edge cases. Also explanations was really great.
@sz.110
@sz.110 Год назад
what an amazing style of teaching really appreciable. just wow
@purukrishnamurthy2960
@purukrishnamurthy2960 Год назад
You're problem solving skills are amazing...thank you so much for the vids
@satyamgupta6030
@satyamgupta6030 Год назад
tysm alisha, ur videos are the only source I look for please keep them coming in future. ur explainations are just different and very easy to understand.
@yusenpeng
@yusenpeng Год назад
At 14:30, I suddenly realized that why I got time limit exceeded. ALL stations IN BETWEEN are insufficient and I don't need to check any of them. Thank u sm!
@nibaronkumar2484
@nibaronkumar2484 Год назад
WOW, your explanation is lit. 🔥🔥
@arjunprasad1071
@arjunprasad1071 Год назад
Good explanation 👏I had read solutions on leetcode, but I wasn't able to get that part as to why we aren't checking from the starting point again whether we can complete the circuit or not. Thanks for clearing it👌👌
@vineetsrivastava388
@vineetsrivastava388 Год назад
Brilliantly explained...no one can defeat u in explanation...open a coaching institute 😎
@sbunny323
@sbunny323 2 месяца назад
your code is checking the gas value which is greater than the cost value at the respective index , for the example [1,2,3,4,5] and the [3,4,5,1,2] 4 > 1 at index 3 and you didn't use the total to calculate the total of the cost in the first approach but isn't that necessary in order to get correct answer take an example when 4 > 1 the total can be < 0 in that condition the code return ans but it should return -1 or iam i missing something?
@modhugumudiushasri2330
@modhugumudiushasri2330 Год назад
great solution and amazing explanation mam.
@EntertainerOnline
@EntertainerOnline 9 месяцев назад
Awesome solution. Thanks !
@harshwardhanchougule3039
@harshwardhanchougule3039 Год назад
Thanks! Very well explained
@abhisheksharma8757
@abhisheksharma8757 2 года назад
Good explanation
@marvel438
@marvel438 6 месяцев назад
Very helpful
@whatwelearntoday
@whatwelearntoday Год назад
Great explanation!
@anupam6045
@anupam6045 2 года назад
woho!! great explanation
@BACSShaileshShettar
@BACSShaileshShettar 8 месяцев назад
u made it so easy!
@maripivenumadhav605
@maripivenumadhav605 2 года назад
nice explanation mam🤩🤩 which company are you working in???
@AINikunjGour
@AINikunjGour 2 года назад
microsoft
@aqua6150
@aqua6150 2 года назад
Nice Explaination..
@aryannagraj4681
@aryannagraj4681 2 года назад
i think i will get placement because of you🤪
@probabilitycodingisfunis1
@probabilitycodingisfunis1 2 года назад
Haha..good luck Aryan! Wish the best 🥳
@rosonerri-faithful
@rosonerri-faithful 2 года назад
Fine explanation diii
@prasenjitdaripa9492
@prasenjitdaripa9492 Год назад
Didi tussi great ho !!!
@santoshbhupati3579
@santoshbhupati3579 Год назад
Thank you
@Idukhan-jj9kc
@Idukhan-jj9kc 2 года назад
Tqs alli
@beonthego8725
@beonthego8725 Год назад
I love you
@aryansonwani7061
@aryansonwani7061 Год назад
Ez question
@pravhatray7418
@pravhatray7418 2 года назад
Di DSA padhaa do mujee bohot tension hai
@maharajak6578
@maharajak6578 3 года назад
First
@shubhamrao5952
@shubhamrao5952 3 года назад
Yeh kya hai??? Samjhaya kuch aur code chaap diya kuch aur??
@shubhamrao5952
@shubhamrao5952 3 года назад
Giving dislike for poor explanation of code .
@007yashkumar2
@007yashkumar2 2 года назад
larchat hai ka
@_S_AmitPandey
@_S_AmitPandey 2 года назад
shi to tha, jo samjhaya wahi code kiya h probably you should have watched the video carefully
@stormshadow76
@stormshadow76 2 года назад
@@shubhamrao5952 if you can't solve atleast let's encourage for someone who teaches us...
@dhruvsingh5190
@dhruvsingh5190 Год назад
Jo explain kiya hai usi ko code kiya hai, shi to hai🤷🏻‍♂️🤷🏻‍♂️
@Rajat_maurya
@Rajat_maurya 2 года назад
thank you
Далее
Leetcode Decode Ways || Intuition + Code + Explanation
31:49
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59
На самом деле, все не просто 😂
00:45
Circular tour problem
9:26
Просмотров 60 тыс.
Non-overlapping Intervals #Leetcode 435 Code C++
18:35
КОГДА К БАТЕ ПРИШЕЛ ДРУГ😂#shorts
00:59