Minimum Cost For Tickets - Leetcode 983 - Python

  Рет қаралды 6,242

NeetCodeIO

NeetCodeIO

Күн бұрын

Пікірлер: 26
@bhoumik8548
@bhoumik8548 3 күн бұрын
The solution becomes much easier when you know what not to do, rather than just knowing what to do. Ur videos are the only thing i watch when i dont feel like doing leetcode.
@ajitpalsingh606
@ajitpalsingh606 3 күн бұрын
same
@johnniewalkerjohnniewalker2459
@johnniewalkerjohnniewalker2459 3 күн бұрын
Happy New Year with a lot of health!!!
@qsvui
@qsvui 3 күн бұрын
what happened to the little drawings you'd make for your thumbnails? those were cute!
@business_central
@business_central 2 күн бұрын
agree! missed them
@NeetCodeIO
@NeetCodeIO 3 күн бұрын
Added a Big-O cheatsheet to NeetCode today: neetcode.io/courses/lessons/big-o-notation It's free and mobile friendly.
@NeetCodeIO
@NeetCodeIO 3 күн бұрын
Here's the code from today's problem in other languages: neetcode.io/solutions/minimum-cost-for-tickets
@CodeSnap01
@CodeSnap01 3 күн бұрын
why here?
@yashwanthsai762
@yashwanthsai762 2 күн бұрын
Thank you
@rohitkumaram
@rohitkumaram 2 күн бұрын
16:40 some people don't like it, I am that some people. but I like your explanation, order doesn't matter when u explain it so well.
@travian821
@travian821 2 күн бұрын
I tried to keep track of the "free days" that a 7 days or 30 days pass would allow you in the dynamic programming cache. The run time increased a lot. I struggle with defining the minimum necessary of variables to solve a problem, i keep some just in case.
@MP-ny3ep
@MP-ny3ep 3 күн бұрын
Very beautifully explained. Thank you
@arunitbaidya6118
@arunitbaidya6118 3 күн бұрын
the goat back at it again
@nicholaschow5734
@nicholaschow5734 2 күн бұрын
Regarding the greedy approach w/ two decisions, I do not think it will work. Take this example: days = {1, 5, 12, 30} tickets= {2, 3} Purchasing a day pass for each day we get 2+2+2+2 = 8 [ One ticket per day ] Purchasing a 7 day pass b/c it's larger: 3 + 3 + 3 = 21 [ Buy on the 1st day, 12th day, then 30th day ] In this situation, choosing the smaller ticket each time is far superior than getting the 7 day pass. If greedy approach is to purchase the larger day pass then it will be insufficient if the next time you need a ticket exceeds the number of valid days for the ticket ( i.e. more than 7 days between traveling)
@business_central
@business_central 2 күн бұрын
Just sharing an opinion cause you asked about our thoughts yesterday on the re-upload thing. I think it's not necessary as I usually check in both your channels if video is already available for that problem. I think in days were there are problems already addressed, it would be great if you could go back in the daily challenges to one of the problems you skipped as you didn't have time for it, and just do a video on that instead. There are still so many ones in the daily challenges that we skipped and to be honest no one explains as good as you. Would love to see that. A part for this, thank you for all your efforts!
@nihalbhandary162
@nihalbhandary162 3 күн бұрын
Instead of iterating over and over again to find the day within the window. You could use a pointer variable to keep a track of the days which are within 7 and 30. That way this gets more fast. dp = [0]*(len(days)+1) k,j =0,0 dp[0]=0 for i in range(len(days)): while days[i]-days[j]>=30: j+=1 while days[i]-days[k]>=7: k+=1 dp[i+1]=dp[i]+costs[0] dp[i+1] = min(dp[k]+costs[1],dp[i+1]) dp[i+1] = min(dp[j]+costs[2],dp[i+1]) #print(dp) return dp[len(days)]
@Hoppitot
@Hoppitot 2 күн бұрын
I iterated for the 7 days but used binary search for the 30 day. I guess this is another way.
@nihalbhandary162
@nihalbhandary162 2 күн бұрын
@@Hoppitot That's what I thought initially to use binary search. But then I remembered if the for some ith value, the maximum extent for 7 day window is j, then for (i+1)th value, it is going to be somewhere right of j. I dont need to use binary search for such small windows.
@tejas1531
@tejas1531 2 күн бұрын
Thanku Bhaiya. I thought the top down but not much properly so i won't be able to code it. But after 1hour and 30 min i came up with bottom up. It helped me to understand how i could have thought for the top down so thank you
@viveksingh9223
@viveksingh9223 3 күн бұрын
It's like a variation of coin change.
@VarshilNarola
@VarshilNarola 3 күн бұрын
I think most efficient solution is O(N) time where N is length of days and O(30+7) space which is technically O(1) space but your solution is O(N*30) time and O(N) space which in this case is worse then logN factor
@NeetCodeIO
@NeetCodeIO 2 күн бұрын
It depends on the input doesnt it? The O(n) you describe is actually O(365), where as my n is technically the actual input size, which will often be less than 365. That's why the runtime of my algo was still efficient.
@VarshilNarola
@VarshilNarola 2 күн бұрын
@ no brother my N is length of days I have mentioned clearly. It is input only. So my solution will work even if days[i]>10^9. In below month and week are only pointers and will atmost traverse through days at max once. So time complexity is O(N) where N is length of days and space complexity is O(30) ~ O(1) N=len(days) dp=[0]*(30) month=week=N for i in range(N-1,-1,-1): while days[i]+30-1
@NeetCodeIO
@NeetCodeIO 2 күн бұрын
@@VarshilNarola oh okay understood, yeah i think this optimization is covered in the code solutions on NC neetcode.io/solutions/minimum-cost-for-tickets i thought you were talking about the max(days) solution
@VarshilNarola
@VarshilNarola 2 күн бұрын
@@NeetCodeIO Yes it is covered and solutions are quiet comprehensive. Thank you!!
@RayTracingX
@RayTracingX 2 күн бұрын
While I have solved 250+ questions on LeetCode, this question is extremely difficult for me, also code part seemed tricky, I spent 40 minutes to only understand problem description😞😞😞
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
1% vs 100% #beatbox #tiktok
01:10
BeatboxJCOP
Рет қаралды 67 МЛН
99.9% IMPOSSIBLE
00:24
STORROR
Рет қаралды 31 МЛН
Minimum Cost For Tickets | Leetcode 983
20:31
Techdose
Рет қаралды 2,4 М.
70% of Programmers can't solve this LeetCode Easy Question
7:32
it's time for a change
6:26
ThePrimeTime
Рет қаралды 189 М.
Leetcode 3404 | Count Special Subsequences | Weekly Contest 430
13:57
Making an Algorithm Faster
30:08
NeetCodeIO
Рет қаралды 185 М.
I Took Three Italian Chefs to China's Famous Noodle Province
12:15
saintcavish
Рет қаралды 384 М.
Dynamic Programming isn't too hard. You just don't know what it is.
22:31
DecodingIntuition
Рет қаралды 230 М.
The Honey Scam: Explained
10:53
Marques Brownlee
Рет қаралды 3,9 МЛН