this is the best solution to this problem ive seen so far!
@KnowledgeCenter Жыл бұрын
Thanks.
@sahilanower91893 жыл бұрын
There's a reason that KZbin recommendations are no doubt the best! 👌🔥
@KnowledgeCenter3 жыл бұрын
Thanks.
@aminurrahaman31863 жыл бұрын
Ki re vai ..same jaigai ese porlam to
@Jess-xx9ul4 жыл бұрын
Your videos have helped someone like me (coding noob) so much, thank you for the clear explanations everyday!
@janmichaelaustria6204 жыл бұрын
seconded! your guidance has taken from zero to hero (well a little bit more than zero :))
@oussamamoussaoui71312 жыл бұрын
we can take the advantage that the array is sorted and use lower_bound() (which is O(log(n))), instead of a loop, (O(n)) to get the index of day7/day30: something like: d7 = lower_bound(days.begin() + i, days.end(), days[i] + 7) - days.begin();
@ankoor4 жыл бұрын
Python: Similar Bottom-up Approach (solving from left to right) def mincostTickets(days, costs): n = len(days) T = [float('inf')] * n T[0] = min(costs) for i in range(1, n): w = i m = i while w >= 0 and days[i] - 7 < days[w]: w -= 1 while m >= 0 and days[i] - 30 < days[m]: m -= 1 T[i] = min(costs[0] + T[i-1], min(costs[1] + T[w], costs[2] + T[m])) return T[n-1]
@LuckyCatTom3 жыл бұрын
FYI: It's incorrect, you can check it on the leetcode
@siddharthsingh43304 жыл бұрын
last_day = days[-1] dp = [0 for _ in range(last_day + 1)] days = set(days) for i in range(1, last_day + 1): if i in days: one = costs[0] + dp[i - 1] seven = costs[1] + dp[max(i - 7, 0)] thirty = costs[2] + dp[max(i - 30, 0)] dp[i] = min(one, seven, thirty) else: dp[i] = dp[i - 1] return dp[-1]
@jimwoodward72934 жыл бұрын
The DP strategy you used is very interesting. Are there any resources you can recommend to learn more about programming the bottom up approach? Preferably in Python. Thanks -- enjoy your videos.
@nands44104 жыл бұрын
Why do you prefer bottom up? Can you elaborate a bit more, Whenever I see overlapping subproblems I can only think of top down
@KnowledgeCenter4 жыл бұрын
I like to think about smallest problem solve it, i.e., base case. Then like to make progress. Then think of generic case (i, ..) and what do I need to find optimal solution for this state in terms of smaller values. So, conceptually this is same as finding recursive relationship of higher i,j...etc in terms of smaller values.
@JardaniJovonovich1924 жыл бұрын
In Top down approach apart from extra table space of O(n), we would even require additional space for recursion stack. In Bottom up approach we do not require that extra space for recursion stack.
@satyaprakashyadav23884 жыл бұрын
I watch your videos almost everyday. Thanks for making such insightful explanations. I want to know one thing from you : Which accessories do you use to create these videos. Please answer in detail. Loads of love to you for your amazing content.
@satyaprakashyadav23884 жыл бұрын
Can You please reply to this. I'll be very grateful to you sir. Simply tell me about the pen you use , which board,mike and recording etc.
@rmadhavmca14 жыл бұрын
I think if you add the days into Hashset, might avoid the while loop.
@WKogut Жыл бұрын
you should have used more descriptive variable names
@BeatrizOliveira-yx4di4 жыл бұрын
Why are you assigning the elements in your vector to 365*costs[0]? For me it works just fine assigning them to 0
@KnowledgeCenter4 жыл бұрын
You are right, we never used those values apart from (n+1)th value i.e., dp[n] = 0 . We assigned 3 values to dp[i] corresponding to day, week, and month and picked min() from there. I was earlier thinking to pick min of initial value with new ones. Ignore that initialization.