Minimum Cost For Tickets | LeetCode 983 | C++, Java, Python

  Рет қаралды 14,362

Knowledge Center

Knowledge Center

Күн бұрын

Пікірлер: 22
@acceleratorlevel645
@acceleratorlevel645 Жыл бұрын
this is the best solution to this problem ive seen so far!
@KnowledgeCenter
@KnowledgeCenter Жыл бұрын
Thanks.
@sahilanower9189
@sahilanower9189 3 жыл бұрын
There's a reason that KZbin recommendations are no doubt the best! 👌🔥
@KnowledgeCenter
@KnowledgeCenter 3 жыл бұрын
Thanks.
@aminurrahaman3186
@aminurrahaman3186 3 жыл бұрын
Ki re vai ..same jaigai ese porlam to
@Jess-xx9ul
@Jess-xx9ul 4 жыл бұрын
Your videos have helped someone like me (coding noob) so much, thank you for the clear explanations everyday!
@janmichaelaustria620
@janmichaelaustria620 4 жыл бұрын
seconded! your guidance has taken from zero to hero (well a little bit more than zero :))
@oussamamoussaoui7131
@oussamamoussaoui7131 2 жыл бұрын
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();
@ankoor
@ankoor 4 жыл бұрын
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]
@LuckyCatTom
@LuckyCatTom 3 жыл бұрын
FYI: It's incorrect, you can check it on the leetcode
@siddharthsingh4330
@siddharthsingh4330 4 жыл бұрын
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]
@jimwoodward7293
@jimwoodward7293 4 жыл бұрын
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.
@nands4410
@nands4410 4 жыл бұрын
Why do you prefer bottom up? Can you elaborate a bit more, Whenever I see overlapping subproblems I can only think of top down
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
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.
@JardaniJovonovich192
@JardaniJovonovich192 4 жыл бұрын
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.
@satyaprakashyadav2388
@satyaprakashyadav2388 4 жыл бұрын
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.
@satyaprakashyadav2388
@satyaprakashyadav2388 4 жыл бұрын
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.
@rmadhavmca1
@rmadhavmca1 4 жыл бұрын
I think if you add the days into Hashset, might avoid the while loop.
@WKogut
@WKogut Жыл бұрын
you should have used more descriptive variable names
@BeatrizOliveira-yx4di
@BeatrizOliveira-yx4di 4 жыл бұрын
Why are you assigning the elements in your vector to 365*costs[0]? For me it works just fine assigning them to 0
@KnowledgeCenter
@KnowledgeCenter 4 жыл бұрын
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.
Pancake Sorting | LeetCode 969 | C++, Java, Python
19:09
Knowledge Center
Рет қаралды 13 М.
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 128 МЛН
快乐总是短暂的!😂 #搞笑夫妻 #爱美食爱生活 #搞笑达人
00:14
朱大帅and依美姐
Рет қаралды 9 МЛН
Random Emoji Beatbox Challenge #beatbox #tiktok
00:47
BeatboxJCOP
Рет қаралды 68 МЛН
Twin Telepathy Challenge!
00:23
Stokes Twins
Рет қаралды 65 МЛН
House Robber | LeetCode 198 | C++, Java, Python
18:45
Knowledge Center
Рет қаралды 11 М.
Random Point in Non-overlapping Rectangles | LeetCode 497 | C++, Python
26:35
983. Minimum Cost For Tickets - LeetCode Python Solution
10:37
Code with Carter
Рет қаралды 465
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Edit Distance | Dynamic Programming | LeetCode 72 | C++, Java, Python
27:22
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 233 М.
(Remade) MinCost Ticket | Dynamic Programming |  Leetcode 983
7:48
Nideesh Terapalli
Рет қаралды 7 М.
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 128 МЛН