Maximum Profit in Job Scheduling | Recursion | Memoization | Leetcode 1235

  Рет қаралды 17,966

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 130
@arthurlewin1952
@arthurlewin1952 Жыл бұрын
Another video another great explanation The way you take us from brute force to optimized solution is phenomenal
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot ❤️❤️❤️
@aws_handles
@aws_handles 9 ай бұрын
Last year i made a huge mistake of taking a paid course which taught me nothing for my intuition building. This year i will practice on my own and be better at DSA
@Vansh-re4jx
@Vansh-re4jx 9 ай бұрын
which couse was it
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
The man with magical voice. Another DP problem made easy. Thank you so much ✌
@TheBikerDude2024
@TheBikerDude2024 23 күн бұрын
Excellent explaining, as always. Kudos to you on making us understand to break complex problems into smaller problems.
@ReenaRote
@ReenaRote 9 ай бұрын
This is the best question to learn/refresh every crucial concept: sorting, binary search, and dynamic programming. Nicely explained as usual.
@sunilsarode152
@sunilsarode152 5 ай бұрын
I was stuck on how to find next job and then I saw your video, clean !!
@satyamgupta1416
@satyamgupta1416 Ай бұрын
Instead of binary search we can carry one more variable prev which initialise with 1 and if we are taking any job then we will update prev as per its endtime.
@ugcwithaddi
@ugcwithaddi 9 ай бұрын
Insane explanation. It doesn’t seem Hard anymore 🔥🙌
@satyasanjay1339
@satyasanjay1339 Жыл бұрын
It did not feel like I was solving a hard problem. your explanation is just awsome.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot ❤️🙏😇
@satyasanjay1339
@satyasanjay1339 Жыл бұрын
@@codestorywithMIK I have a request. can you please make a video on the maximum number of events that can be attended? It is a leetcode medium problem. I couldn't understand its optimal solution.
@codestorywithMIK
@codestorywithMIK Жыл бұрын
@satyasanjay1339 are you referring to the first part of today’s Leetcode POTD ?
@satyasanjay1339
@satyasanjay1339 Жыл бұрын
@@codestorywithMIK yes
@Ramneet04
@Ramneet04 9 ай бұрын
I tried this with curr and prev approach but got tle then came to this vedio, we actually use this in approach where we sort in other questions i guess. I was really helpful ❤❤
@abhinavtomar8518
@abhinavtomar8518 Жыл бұрын
Really well explained bro, Doesn't feel it was a hard question. Really impressed the way you explain keep bringing such videos thank you
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a ton ❤️❤️😇
@ishankiverma9450
@ishankiverma9450 Жыл бұрын
Very nicely explained..i request you to teach some advanced topics in DSA too!!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Soon Ishanki. Thanks a lot 😇🙏
@codeyoga
@codeyoga 9 ай бұрын
Good question to apply Binary Search and Recursion with Memo. Very nicely explained, keep it up !
@amankushwaha8180
@amankushwaha8180 7 ай бұрын
your explanation are very good
@sourabhbindal774
@sourabhbindal774 9 ай бұрын
Thanks, MIK Sir here is the Bottom-up solution derived from memoization- class Solution { public: int getIndex(vector& arr, int val, int l, int r) { int res = r + 2; while (l
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
I feel so happy and proud that we are now able to derive bottom also. Thanks a lot for sharing 😇🙏❤️
@shoaibakhtar9194
@shoaibakhtar9194 6 ай бұрын
Best and easy explanation of this question
@nadeking1530
@nadeking1530 9 ай бұрын
As always awesome explanation , where would we go without you lol!
@nitinkaplas1532
@nitinkaplas1532 Жыл бұрын
bhai yrr isse tarah question solve kroge tu 1 saal me subscriber i think 1Million + ho jaege 😄
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Means a lot Nitin ❤️❤️😇😇
@the_only_one9415
@the_only_one9415 16 күн бұрын
Hello sir, here how we can get intuition of combining all arrays into single vector?
@chillkro25
@chillkro25 8 ай бұрын
Bhaiya prev name kaa variable bna ke uske basis pr.next job ko take/not take kr lete , ye approach chalega ya nhi , like longest increasing subsequence me prev variable bna ke krte h , pls reply
@cocoavlogs6622
@cocoavlogs6622 26 күн бұрын
please make for knapsack too 0/1 and unbounded
@soumyasatish7941
@soumyasatish7941 9 ай бұрын
Hello! I have been trying to calculate the next index using the stl lower_bound function from index i+1 till end but keep getting syntax errors for int next = lower_bound(array.begin() + i + 1, array.end(), array[i][1]) - array.begin(); I know the problem is with array.begin + i + 1 but don't know what to replace it with. Let me know your thoughts on this.
@Raj10185
@Raj10185 9 ай бұрын
yes can be done in this way :- class Solution { public: int dp[50004]; vector< pair< int,pair > > arr; int solve(int i ,vector & starr ) { //base case if(i == starr.size()) return 0; if(dp[i]!= -1) return dp[i]; int nottake = 0 + solve(i+1 ,starr ); int take = -1e9; int nxt_idx = lower_bound(starr.begin(),starr.end(),arr[i].second.first) - starr.begin(); take = arr[i].second.second + solve(nxt_idx,starr); return dp[i] = max(take,nottake); } int jobScheduling(vector& startTime, vector& endTime, vector& profit) { int n = startTime.size(); // vector dp(n+2,-1); memset(dp,-1,sizeof(dp)); //sort accng to the start time first for(int i = 0 ; i < n ; i++) { arr.push_back({startTime[i], {endTime[i], profit[i]}}); } sort(arr.begin(),arr.end()); vectornewArrayIndex; for(int i=0;i
@soumyasatish7941
@soumyasatish7941 9 ай бұрын
@@Raj10185 I want the lower_bound function to search from the index after i till end. Trying to reduce the search space for lower_bound
@Raj10185
@Raj10185 9 ай бұрын
@@soumyasatish7941 yes you can do but its already binary search so doesn.t matter much O (logn} is best tc
@shivammaurya5464
@shivammaurya5464 9 ай бұрын
bahut mast explain kiya hai
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Means a lot ❤️❤️🙏🙏
@vishalplayzz2580
@vishalplayzz2580 9 ай бұрын
sorting based on ending time giving us wrong answer ? we will be finding nextind from the current index through binary search ending time sorting not making sure that it is sorted and starting time sorting making sure it is sorted when finding nxt ind ? is that it ?
@humanity7880
@humanity7880 Жыл бұрын
Thank you for this amazing explanation!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot for watching 😇❤️
@tarunsingh6374
@tarunsingh6374 9 ай бұрын
Bhaiya , please try to explain java code also 🤌
@sidharthdhiman4522
@sidharthdhiman4522 Жыл бұрын
sir can you also explain time complexity at the end also for more beeter understanding
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure thing Sidharth. Noted ❤️❤️
@deepakdass4710
@deepakdass4710 9 ай бұрын
Thanks sir for great explanation ❤❤
@chetanyagupta5801
@chetanyagupta5801 9 ай бұрын
can we also sort based on end time?
@siriusblack88
@siriusblack88 Жыл бұрын
Great expectation
@keshavmn7283
@keshavmn7283 Жыл бұрын
Really good explanation 👍
@gauravbanerjee2898
@gauravbanerjee2898 9 ай бұрын
Thanks for the explanation Bhaiya but why did you took result as n+1 ??
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Good qn. Actually if you notice, whatever index you return from getNextIndex function ,you pass that index in recursion call i.e. int next = getNextIndex(array, i+1, array[i][1]); int taken = array[i][2] + solve(array, next); So, if you get no valid element from getNextIndex , I am returning n+1, because when I will call int taken = array[i][2] + solve(array, next); (with next = n+1), I will return from the base case if(i >= n) return 0; You can return any invalid value from getNextIndex in case you don't find any valid element BUT, make sure you handle it in base case.
@gauravbanerjee2898
@gauravbanerjee2898 9 ай бұрын
@@codestorywithMIK Got it thanks a lot for the clarification bhaiya ❤
@vartikatiwari5203
@vartikatiwari5203 Жыл бұрын
Great sir,Keep going
@AlphaOmega-v7k
@AlphaOmega-v7k 3 ай бұрын
For all those stuck with runtime error-Do not use comparator for sorting use normal sorting that will work fine.
@sz.110
@sz.110 Жыл бұрын
La jawaab ❤‍🔥❤‍🔥❤‍🔥
@yogeshkumarverma2125
@yogeshkumarverma2125 11 ай бұрын
Bhai bohot bohot dhanyawad❤
@10minutes_cs
@10minutes_cs 4 ай бұрын
I am always get confused when to late l
@utsavbarnwal7120
@utsavbarnwal7120 Жыл бұрын
Such A great Explanation ☺
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@piyushsrivastava5883
@piyushsrivastava5883 9 ай бұрын
bhaiya simple knapsack approach kyu nhi lagega
@zaviyakhurshid9097
@zaviyakhurshid9097 Жыл бұрын
Awesome Explanation
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Zaviya 😊
@nisarggogate8952
@nisarggogate8952 Жыл бұрын
Awesome explanation!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Nisarg ❤️❤️❤️
@deepakarya5310
@deepakarya5310 9 ай бұрын
sir if we change the result value then we are getting overflow exception
@bhuppidhamii
@bhuppidhamii 9 ай бұрын
thank you
@abc-ym4zs
@abc-ym4zs Жыл бұрын
sir can u i dont know how to write comp function generally we will write as bool comp(auto v1,auto v2) { return v1[0]
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi there, I have started a playlist where I will be teaching comparators, STLs etc. - kzbin.info/aero/PLpIkg8OmuX-KtmrBzx-dAzRDp0xn_Xjls Also, you can see this resource that I have created on Github - github.com/MAZHARMIK/Cpp-STL-Quick-Help
@AmandeepSingh-uq3wp
@AmandeepSingh-uq3wp Жыл бұрын
Great Explanation 👍
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️❤️ thank you Amandeep
@umeshbisht1054
@umeshbisht1054 9 ай бұрын
Thanku bhaiya ❤
@shivkumar-og4ow
@shivkumar-og4ow Жыл бұрын
bhaiya leetcode contest solution bhi upload kro
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Sure Shiv, soon planning
@Saryupareen
@Saryupareen Жыл бұрын
what an explaination great!!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much 😇❤️
@tanishchordia4813
@tanishchordia4813 6 ай бұрын
Hello mik , today I tried the exact solution which you taught in the above video but the last testcase gave runtime error. I thought maybe i missed somthing so just copy pasted your solution and your soltuion also gave the runtime error on the last testcase can you help me?
@AlphaOmega-v7k
@AlphaOmega-v7k 3 ай бұрын
don't use comparator fubnction for sorting use normal sorting
@satvrii
@satvrii Жыл бұрын
❤❤❤❤
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️
@manashchoudhary744
@manashchoudhary744 8 ай бұрын
Which software u r using
@satyaprakashtiwari5494
@satyaprakashtiwari5494 Жыл бұрын
Thanks for making this
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️😇😇😇
@anmolraj5054
@anmolraj5054 9 ай бұрын
Bro you r gem
@justgoahead_123
@justgoahead_123 9 ай бұрын
awesome bro !! : )
@keertilata20
@keertilata20 Жыл бұрын
hehe i am so happy i did on my own.... int solve(vector &arr, int ind, vector &dp){ int n=arr.size(); if(ind >= n){ return 0; } if(dp[ind] != -1){ return dp[ind]; } int nxtInd = lower_bound(arr.begin(), arr.end(), vector{arr[ind][1], 0, 0}) - arr.begin(); int take = arr[ind][2] + solve(arr,nxtInd,dp); int notTake = 0 + solve(arr,ind+1,dp); return dp[ind] = max(take,notTake); } int jobScheduling(vector& startTime, vector& endTime, vector& profit) { int n=startTime.size(); vector arr(n, vector(3, 0)); //{s, e, p} for(int i = 0; i
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Awesome 👌✌️
@wearevacationuncoverers
@wearevacationuncoverers 9 ай бұрын
This is the only place I get me doubts answered. Thanks mik sir
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
nice
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Vishal ❤️
@cacklouncle
@cacklouncle Жыл бұрын
Badhiya
@codestorywithMIK
@codestorywithMIK Жыл бұрын
❤️❤️
@035-harshitsingh7
@035-harshitsingh7 Жыл бұрын
bhaiya aap bottom up approach se kyun nhi btate-- tabular wala form
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Hi Harshit, Actually i will cover all ways to solve DP in my DP concepts and Qns playlist - kzbin.info/aero/PLpIkg8OmuX-JhFpkhgrAwZRtukO0SkwAt
@shashanksaurabh6478
@shashanksaurabh6478 4 ай бұрын
Isn't it a greedy?
@CodeMode9313
@CodeMode9313 8 ай бұрын
mast maggaan video ::)
@VinayKumar-rq5kd
@VinayKumar-rq5kd 9 ай бұрын
why did you initiliaze n +1 to result. is it n.
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Good qn. Actually if you notice, whatever index you return from getNextIndex function ,you pass that index in recursion call i.e. int next = getNextIndex(array, i+1, array[i][1]); int taken = array[i][2] + solve(array, next); So, if you get no valid element from getNextIndex , I am returning n+1, because when I will call int taken = array[i][2] + solve(array, next); (with next = n+1), I will return from the base case if(i >= n) return 0; You can return any invalid value from getNextIndex in case you don't find any valid element BUT, make sure you handle it in base case.
@bhuppidhamii
@bhuppidhamii 9 ай бұрын
classic dp
@harshtiwari416
@harshtiwari416 9 ай бұрын
Bottom Up video????
@bhartendupant8859
@bhartendupant8859 9 ай бұрын
@nagmakhan3165
@nagmakhan3165 Жыл бұрын
Phenomenal
@anshikagupta-l1r
@anshikagupta-l1r 26 күн бұрын
How this solution is logically wrong? int job(int ind,vector& st, vector& en, vector& p,int end) { if(ind>=st.size())return 0; if(end
@abhishekverma7604
@abhishekverma7604 Жыл бұрын
bhya mujhe isme fractional knapsack ki vibe aa rhi hai...😂
@roshangeorge97
@roshangeorge97 11 ай бұрын
can we use greedy?
@souravjoshi2293
@souravjoshi2293 9 ай бұрын
Let's consider an example to illustrate why a greedy approach may fail for this problem : Consider the following jobs: Job: A B C Start: 1 2 3 End: 3 5 6 Profit:10 5 8 If we sort the jobs by their end times in ascending order, we get: Job: A B C Start: 1 2 3 End: 3 5 6 Profit:10 5 8 A greedy approach might consider selecting jobs based on the highest profit at each step. If we follow this greedy strategy, we might select Job A and Job C, as they have the highest profits. However, the optimal solution is to select Job B and Job C, which results in a total profit of 13. The greedy approach would lead to a suboptimal solution in this case.
@MakeuPerfect
@MakeuPerfect Жыл бұрын
bhaiya wo duplicate wala akbar samjhate kya hua bsearch mein
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Take this example : {start, end, profit} { {1, 3, 50}, {2, 4, 10}, {3, 5, 40}, {3, 6, 70} } Suppose you chose to do job 1 - > {1, 3, 50}, Now, you need choose next job whose starting point is >= to ending point of your 1st job {1, 3, 50} So, you have two options {3, 5, 40}, {3, 6, 70} Using binary search, the mid might point to {3, 6, 70}, but you need to find the first job which has starting point >= your 1st job end point. That's why if my mid points to {3, 6, 70}, I still try to go left (r = mid-1) and see if we can get more task to left which has starting point >= end point of 1st job Hope this helps
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
@@codestorywithMIK Yeah correct. Since we want to try all possibilities, we don't want to miss {3, 5, 40} and would try with it and then later try with {3, 6, 70}. Right ?
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Or I think we should try to do the dry run for this example , it will help : [6,15,7,11,1,3,16,2] [19,18,19,16,10,8,19,8] [2,9,1,19,5,7,3,19]
@debanga3501
@debanga3501 Жыл бұрын
@@codestorywithMIK Ek no explanation
@MakeuPerfect
@MakeuPerfect Жыл бұрын
@@codestorywithMIK at first mid is pointing to 3/2=1 index then we find the mid is mid+1 to till the end and mid pointing to 5/2=2index, So mid point to {3,5,40}.
@AmarjeetKumar-to9ub
@AmarjeetKumar-to9ub 9 ай бұрын
🔥++
@ChiragWadwa
@ChiragWadwa 9 ай бұрын
pls help me find the start mistake , isme maine lower bound use kia , galat kyu aa rha h , pls help!!! class Solution { public: int getNext(int start,int target,vector &arr){ auto it = lower_bound(arr[0].begin()+start,arr[0].end(),target); int ind = it - arr[0].begin(); return ind; } int solve(int curr,vector &arr,vector &dp){ if(curr >= arr.size()) return 0; if(dp[curr] != -1) return dp[curr]; //take int nxt = getNext(curr+1,arr[curr][1],arr); int take = arr[curr][2] + solve(nxt,arr,dp); //notTake int notTake = solve(curr+1,arr,dp); return dp[curr] = max(take,notTake); } int jobScheduling(vector& startTime, vector& endTime, vector& profit) { int n = startTime.size(); vectorarr(n,vector(3)); for(int i=0 ; i
@wearevacationuncoverers
@wearevacationuncoverers 9 ай бұрын
If you want to use lower_bound to find the first job whose starting point is greater than or equal to currentJobEnd, the condition should be vec[0] < currentJobEnd in the lambda function of lower_bound Something like this : int getNextIndex(vector& array, int l, int currentJobEnd) { auto it = lower_bound(array.begin() + l, array.end(), currentJobEnd, [](const vector& vec, int val) { return vec[0] < val; }); return it - array.begin(); } This will work.
@ChiragWadwa
@ChiragWadwa 9 ай бұрын
@@wearevacationuncoverers thanks a ton bhay ...but can u tell why my code was failing???
@ChiragWadwa
@ChiragWadwa 9 ай бұрын
@@wearevacationuncoverers and also can u tell how u exactly wrote this compator thing??? , ive seen miks video of compaator , but still i am not able to get ,, cn u help me out pls?
@VedBrahmbhattBEE
@VedBrahmbhattBEE 9 ай бұрын
Can Someone tell me why my code is giving overflow runtime error ? class Solution { public: int n, dp[50001]; int getnext(vector& arr, int l, int currentjobend) { int r = n - 1; int ans = n + 1; while (l = currentjobend) { ans = mid; r = mid - 1; } else { l = mid + 1; } } return ans; } int solve(vector& arr, int i) { if (i >= n) return 0; if (dp[i] != -1) return dp[i]; int nextjob = getnext(arr, i + 1, arr[i][1]); int take = arr[i][2] + solve(arr, nextjob); int nottake = solve(arr, i + 1); return dp[i] = max(take, nottake); } int jobScheduling(vector& startTime, vector& endTime, vector& profit) { n = startTime.size(); memset(dp, -1, sizeof(dp)); vector arr(n, vector(3, 0)); for (int i = 0; i < n; i++) { arr[i][0] = startTime[i]; arr[i][1] = endTime[i]; arr[i][2] = profit[i]; } auto comp = [&](auto& vec1, auto& vec2) { // to sort array based on the starting element return vec1[0]
@user-le6ts6ci7h
@user-le6ts6ci7h 9 ай бұрын
Hey MIk I have been looking for jobs , will you mind giving me a referral at your company.
@codestorywithMIK
@codestorywithMIK 9 ай бұрын
Hi there, Unfortunately my company has announced Hiring freeze due to bad market condition. But feel free to share your resume with me on my email id (you can find email from my github profile), I will try my best to share it with my friends and colleagues to check.
@ishabharwal1699
@ishabharwal1699 9 ай бұрын
hi can you please tell me whats wrong in my solution?? take condition class Solution { public: int recur(int idx,int last,vector& st, vector& et, vector& profit){ if(idx==profit.size()){return 0;} int take=0; if(last
@Chakss
@Chakss Жыл бұрын
Amazing explanation!
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you 😊
@ispyu1514
@ispyu1514 Жыл бұрын
Great explanation!
@ankushdangi3154
@ankushdangi3154 Жыл бұрын
❤❤
@pavankumareluri9226
@pavankumareluri9226 Жыл бұрын
nice
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you Pavan ❤️🙏😇
Maximum Profit in Job Scheduling - Leetcode 1235 - Python
14:45
NeetCodeIO
Рет қаралды 33 М.
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 94 МЛН
How to whistle ?? 😱😱
00:31
Tibo InShape
Рет қаралды 17 МЛН
Sigma baby, you've conquered soap! 😲😮‍💨 LeoNata family #shorts
00:37
L6. Job Sequencing Problem | Greedy Algorithm Playlist
16:07
take U forward
Рет қаралды 43 М.
1235. Maximum Profit in Job Scheduling | DP | Binary Search
17:50
Mastering Dynamic Programming - How to solve any interview problem (Part 1)
19:41
Job Sequencing Problem | Greedy Algorithms
17:00
take U forward
Рет қаралды 160 М.
Maximum Profit in Job Scheduling | Leetcode 1235 | #ayushisharmaDSA
26:30
I Solved 100 LeetCode Problems
13:11
Green Code
Рет қаралды 149 М.
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 94 МЛН