DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥

  Рет қаралды 393,214

take U forward

take U forward

Күн бұрын

Пікірлер: 481
@takeUforward
@takeUforward 2 жыл бұрын
Please give us a like and share this content as much as possible :)
@lapimpale
@lapimpale 2 жыл бұрын
Thank you bro for your hard work to add new videos. Congratulations for completing 2 years on youtube :)
@rechinraj111
@rechinraj111 2 жыл бұрын
When will remaining DP problems will come ?
@avishkarpatil5871
@avishkarpatil5871 8 ай бұрын
8:36 The provided solution is not using dynamic programming. Dynamic programming involves breaking down a problem into smaller subproblems and solving each subproblem only once, storing their solutions to avoid redundant calculations. The solution provided is a simple linear scan through the prices array, keeping track of the minimum price encountered so far (temp) and updating the maximum profit (maxi) accordingly. It doesn't involve breaking down the problem into subproblems or utilizing memorization of intermediate results, which are characteristic features of dynamic programming algorithms. Instead, it employs a straightforward greedy approach to find the maximum profit by considering the difference between each price and the minimum price encountered so far.
@_AmbujJaiswal
@_AmbujJaiswal 5 ай бұрын
true.. it greedy works best for this question
@saatvikmangal7994
@saatvikmangal7994 3 ай бұрын
but Isn't it dividing into sub-problems as well if we carefully observe The for loop at the time of the ith iteration will answer up to that ith iteration. But you are correct in the sense that we are not leveraging this answer to find the next answer, however, we are leveraging minimum value to find the next answer
@priyanshkumar17
@priyanshkumar17 3 ай бұрын
Yes, I agree with you
@muskan_bagrecha
@muskan_bagrecha 2 ай бұрын
Yep agreed. I was confused why this problem was tagged under DP.
@krishnagarg2583
@krishnagarg2583 Ай бұрын
i have recursion function for this question i did u can use it for memoization and tabulation int maxProfitRec(vector& arr, int index, int minPrice, int maxProfit) { int n = arr.size(); // Base case: If we have processed all days, return the maximum profit found if (index == n) return maxProfit; // Update minimum price minPrice = min(minPrice, arr[index]); // Calculate profit if we sell on this day int currentProfit = arr[index] - minPrice; // Update the maximum profit maxProfit = max(maxProfit, currentProfit); // Recursive call for the next day return maxProfitRec(arr, index + 1, minPrice, maxProfit); } int maxProfit(vector& arr) { // Start with the first day's price as the initial minimum price and 0 as initial max profit return maxProfitRec(arr, 0, INT_MAX, 0); }
@rocktatnine
@rocktatnine Жыл бұрын
Hello, thanks for this solution. Lucid & perfectly explained. I have a simple doubt. How does this problem fall in the category of DP. I'm genuinely confused coz my understanding of DP is different. Pls Help.
@TW-uk1xi
@TW-uk1xi 2 жыл бұрын
Because of this guy, I love dynamic programming.
@rahulbhagat4023
@rahulbhagat4023 2 жыл бұрын
Yes u will until u start solving different questions
@ShubhamVerma-hw4uj
@ShubhamVerma-hw4uj 2 жыл бұрын
@@rahulbhagat4023 so these questions are not enough?
@priyanshumohanty5261
@priyanshumohanty5261 2 жыл бұрын
@@rahulbhagat4023 Everyone starts somewhere ig. Many people found these problems ridiculously hard earlier
@nanda_8
@nanda_8 2 жыл бұрын
Codeforces me dp tag lagake 1800 rating laga.. Sara bhukar utar jayega :} BTW no harm to striver bhaiya... He is explaining all the classical problems using which we can solve hard problems with sufficient practice. Watching this whole play list is not at all sufficient. (Hard work from your end is needed)
@ShubhamVerma-hw4uj
@ShubhamVerma-hw4uj 2 жыл бұрын
@@nanda_8 yha 1500 nhi hore h bhai tu 1800 ki baat kr ra hai
@RAKSHITHPGBBTCSBTechCSE
@RAKSHITHPGBBTCSBTechCSE Жыл бұрын
I think u are the best teacher in this whole world...keep it up man...
@AyushSharma-s8t
@AyushSharma-s8t Ай бұрын
haha, i think uu are new on youtube..
@johncenakiwi
@johncenakiwi 2 жыл бұрын
Thanks Striver, I have been stuck on the Buy and Sell stocks with at most k transactions problem for sometime now. Will wait for your video.
@GaneshBhutekar-nu1gd
@GaneshBhutekar-nu1gd 9 күн бұрын
I was unable to figure out the best time to buy and sell stock in part 3. Later, I realized that there is a whole series on this topic. I had actually attempted part 1 of this series before, but I couldn't relate it to part 3.
@samiranroyy1700
@samiranroyy1700 2 ай бұрын
I think u are the best teacher in this whole world...keep it up man... I think u are the best teacher in this whole world...keep it up man... I think u are the best teacher in this whole world...keep it up man...🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰🥰
@stith_pragya
@stith_pragya 9 ай бұрын
UNDERSTOOD.....Thank You So Much for this wonderful video.....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@shashankjagtap5051
@shashankjagtap5051 Жыл бұрын
There are many ways of solving this problem, but your one is the best.!
@AbhiRam-b2k
@AbhiRam-b2k Жыл бұрын
Striver, you had been mentioning space optimization since a lot of videos on this playlist. But isnt it that it takes some amount of time to store the previous variables/vectors for previous rows into a temp vector? This would add up to the computation time right??? And in this era we care more of time reduction than space reduction. Kindly let me know if I am wrong, and if not, then why do we need space optimization? Thank you for your valuable time.
@AnshKumar-tj8bn
@AnshKumar-tj8bn 4 ай бұрын
When using things on cloud, cost is charged effectively on both storage and number of operations/compute power used. Thats why maybe on a larger picture it's effective.
@jaishriharivishnu
@jaishriharivishnu 3 ай бұрын
some of the questions can't be solved without space optimization. let suppose you have n=100 and X=1e6... now if you make int dp[100][1e6] this will cost you 1e8 space... which is not possibe and will give you segmentation error... With "prev" and "cur" space optimization you can do it in 2*X space... i.e order of 6
@RogithRog
@RogithRog 5 ай бұрын
In java, Easy to understand public static void main(String[] args) { int arr [] = {7,1,5,3,6,4}; int temp =0; int max = Integer.MIN_VALUE; for(int i=0;i
@akankshaawasthi3932
@akankshaawasthi3932 Жыл бұрын
In first iteration cost will be -6 so it will update it in cost or not ? Then in max profit it will update it by -6 or not? Or in mini while going i=2 how it will update min?
@shambhaviaggarwal9977
@shambhaviaggarwal9977 23 күн бұрын
Understood, thanks!
@rahul-sinha
@rahul-sinha 2 жыл бұрын
Man, your DP playlist on KZbin will gonna rock Man....!!!!! 🥳
@AnandKumar-og8bu
@AnandKumar-og8bu Жыл бұрын
Bhai main DSA wala banda hun nhi, I'm a pure developer and I hate DSA due to its toughness but randomly today I thought of watching a video of Striver and I'm amazed. I'm able to understand it very well. Hats off to you Striver for your explanation. Live a quality life bro ❤
@yashrajdeshmukh6759
@yashrajdeshmukh6759 Жыл бұрын
I want to know how did you become a pure developer without DSA?
@iamnoob7593
@iamnoob7593 9 ай бұрын
@@yashrajdeshmukh6759 U just need to know basics of DSA , OOPs Thats it .
@bhupendrasinghshahi5933
@bhupendrasinghshahi5933 2 жыл бұрын
understood. Please make a playlist for Greedy too. Thank You.
@torishi82
@torishi82 3 ай бұрын
Nobody explains like you. Awesome.
@hashcodez757
@hashcodez757 2 ай бұрын
"UNDERSTOOD BHAIYA!!"
@arkaprabhasaha8203
@arkaprabhasaha8203 5 ай бұрын
Following your entire DSA A2Z Course!
@imtsrk04
@imtsrk04 11 ай бұрын
4th November 2023, I'm watching this video while seeing Arrays Topic. Commenting here to see how long it takes for me to reach the same video in DP Playlist.
@AyushVerma-wu3nn
@AyushVerma-wu3nn 7 ай бұрын
are you there yet buddy?
@raghavmanish24
@raghavmanish24 2 ай бұрын
video start krne se phle hi solve kr liya tha ....aut by chance same method nikla 😁😁....thanku striver
@keshavbaheti7327
@keshavbaheti7327 25 күн бұрын
Done, will never be coming back to see this video again. Note to self
@samuelfrank1369
@samuelfrank1369 Жыл бұрын
Understood. Thanks a lot. Please upload more videos Bhaiyaaa
@kaichang8186
@kaichang8186 Ай бұрын
Thanks for the great effort
@Hrushi_2000
@Hrushi_2000 Жыл бұрын
Understood. Thankyou Sir
@ivoryAlpaca
@ivoryAlpaca 2 ай бұрын
Striver, you really are a legend
@iEntertainmentFunShorts
@iEntertainmentFunShorts 2 жыл бұрын
This was the one of the toughest question on dp string we came across specially with the base case, and omitting the for loop in Reccurence equation. Thank you so much Striver Bhaiya you have made DP so intuitive like how to think from scratch Thanks you so much again 💗
@prabhakaran5542
@prabhakaran5542 6 ай бұрын
Understood ❤
@shubhamagarwal1434
@shubhamagarwal1434 2 ай бұрын
#Free Education For All.. # Bhishma Pitamah of DSA...You could have earned in lacs by putting it as paid couses on udamey or any other elaerning portals, but you decided to make it free...it requires a greate sacrifice and a feeling of giving back to community, there might be very few peope in world who does this...."विद्या का दान ही सर्वोत्तम दान होता है" Hats Off to you man, Salute from 10+ yrs exp guy from BLR, India..
@UECAshutoshKumar
@UECAshutoshKumar 3 ай бұрын
Thank You! Understood
@KarthikNandam-xs4qn
@KarthikNandam-xs4qn 7 күн бұрын
Understood My G 🔥
@Mythri333
@Mythri333 2 күн бұрын
Thank you ❤
@ganeshjaggineni4097
@ganeshjaggineni4097 2 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@anshulrai6058
@anshulrai6058 Ай бұрын
understood👍
@rishav144
@rishav144 2 жыл бұрын
best DP series ever....thanks Striver 💛
@sanchitdeepsingh9663
@sanchitdeepsingh9663 4 ай бұрын
understood , thanks
@jaackiye
@jaackiye 3 ай бұрын
Understood! thanks !! Long live !!!
@konankikeerthi
@konankikeerthi 4 ай бұрын
Thank you bro. Understood
@ajitpal0821
@ajitpal0821 2 жыл бұрын
We also apply that first find min from array then from that index to end find the maximum element, please reply @take U Forward
@hahahaha4217
@hahahaha4217 Жыл бұрын
Yeah even I got the same idea
@AquaRegia-i3u
@AquaRegia-i3u Жыл бұрын
fails for this case 2 7 1 2. answer is 5 (7-2).
@gautamsaxena4647
@gautamsaxena4647 Ай бұрын
understood bhaiya
@arjunavsaikia6239
@arjunavsaikia6239 6 сағат бұрын
good
@heyOrca2711
@heyOrca2711 7 ай бұрын
Understood Sir!
@reddevilbeast15
@reddevilbeast15 Жыл бұрын
Sir can you please cover Linked List like Array series.Within a month ??
@siyangaming3242
@siyangaming3242 2 жыл бұрын
I don't know DP. but i solved this problem. from basic problem solving skills my aproach was: 1. find the minimum element in array. 2. then find the maximum element in the remaining array from the minimum number position. ex: suppose i found min element 2 at 3rd index then find the max element in the remaining array from 4th index to n; 3. subtract the maximum element - minimum element. BOOM ans is ready
@salilkumar9452
@salilkumar9452 2 жыл бұрын
But this is wrong ex : 2,10,1,4
@sreenivasprasad6538
@sreenivasprasad6538 2 жыл бұрын
What if maximum element is at 0th index and minimum element is at last index. BOOM ans is wrong.
@umashankar12318
@umashankar12318 2 жыл бұрын
@@sreenivasprasad6538 😂
@swastikpatro6436
@swastikpatro6436 Жыл бұрын
😂😂😂
@anndiegeeky6217
@anndiegeeky6217 Жыл бұрын
I also thought but this will be wrong when Array is 1000,2000,999,1002
@adityarao4157
@adityarao4157 2 ай бұрын
Its a simple greedy algo, dont blame him he is just making sure of getting the gist of the questions we ll encounter in this type of pattern
@armaanhadiq3741
@armaanhadiq3741 2 жыл бұрын
By the way Engineering means optimisation so we have to optimise things
@wecan2729
@wecan2729 6 күн бұрын
class Solution { public int maxProfit(int[] arr) { int mn=arr[0]; int n=arr.length; int ans=0; for(int i=1;i
@JatinGupta-ze6nc
@JatinGupta-ze6nc 8 ай бұрын
Bhaiya I m starting today DSA sheet I will mark in comments which day I m seeing your video like day 5 video I will comment in your video day 5completed it help me to make consistent thanks bhaiya
@sahilsaini4574
@sahilsaini4574 6 ай бұрын
how would be able to solve it using recurrence. I'm unable to figure it out
@tester9920tester
@tester9920tester Ай бұрын
please explain with tree backtraking this solution
@as_if
@as_if 3 ай бұрын
My approach was finding the next greatest selling cost, which required 2 loops. And the difference is here we're finding previous smallest buying cost, that is already in the memory. This dropped the time to O(n). We moved our search-for-information from the unexplored area to the already explored area.
@msannitya6144
@msannitya6144 5 ай бұрын
Ur really a saver of my life bhayya
@srinathv1412
@srinathv1412 9 ай бұрын
Mast understood !!!!!!
@oyeesharme
@oyeesharme Ай бұрын
thanks bhaiya
@resetengineering
@resetengineering Жыл бұрын
Wondering, if you should include this in DP playlist
@gentleman7060
@gentleman7060 Жыл бұрын
Why u didn’t do it with recursive dp?
@AnkitSingh-wq2rk
@AnkitSingh-wq2rk 2 жыл бұрын
Bhai i am also a working professional but I wanted to ask itna sab daily kaise karte ho ? office ka kaam phir video recording ya live stream ? burnout nahi feel karte kya ? I am on kinda similar grind of upskilling ... giving contest learning new tech stacks but kabhi kabhi social life ki L lag jate hai ...
@tvrao123
@tvrao123 7 ай бұрын
this solution is wrong for the test case, price = [100,180,260,310,40,535,695]
@suyashshinde2971
@suyashshinde2971 Жыл бұрын
SDE Sheet Day 1 Problem 6 Done!
@shouryadubey2878
@shouryadubey2878 18 күн бұрын
understood
@sarangkumarsingh7901
@sarangkumarsingh7901 7 ай бұрын
Awesome Sir..................
@khalasianiket816
@khalasianiket816 4 ай бұрын
understood ❤
@Hipfire786
@Hipfire786 6 ай бұрын
understood everything
@kartikeymishra5647
@kartikeymishra5647 2 жыл бұрын
never knew that we called this also as dynamic programming 😅
@NonameNoname-f2t
@NonameNoname-f2t 8 ай бұрын
understood sir ! '
@jaishriharivishnu
@jaishriharivishnu 3 ай бұрын
4:00 i will approach this with priority queue
@rohanmadiratta6421
@rohanmadiratta6421 Жыл бұрын
On the A2Z course u have this ques under arrays so how are we supposed to do it without dp?
@LazyCoder20
@LazyCoder20 6 күн бұрын
@rohanmadiratta6421 . I know its late but If you are solving just with arrays knowledge it can be solved this way. " int maxPro = 0; int n = prices.length; for(int i=0;i
@rushyya
@rushyya Жыл бұрын
UNDERSTOOD!
@DeadPoolx1712
@DeadPoolx1712 2 ай бұрын
UNDERSTOOD;
@itzzz48
@itzzz48 11 ай бұрын
Why the lecturers never teach like this during college🙁
@Rob-J-BJJ
@Rob-J-BJJ Жыл бұрын
good stuff buddy
@YourCodeVerse
@YourCodeVerse Жыл бұрын
Understood✅🔥🔥
@vigneshkumar4990
@vigneshkumar4990 5 ай бұрын
understood💙
@damnrish4873
@damnrish4873 Ай бұрын
Understood!!
@jaykumargupta7307
@jaykumargupta7307 2 жыл бұрын
question link given in the desc. box is wrong
@chirag71269
@chirag71269 Ай бұрын
STRIVER 🔥
@saimasyeda6544
@saimasyeda6544 18 күн бұрын
Understood
@linhinNTU
@linhinNTU Жыл бұрын
can anyone explain for me why the space complexity is O(1) like he said?
@niladrisekharnath
@niladrisekharnath 2 жыл бұрын
How is Dynamic Programming ?
@SmartStudyHub27
@SmartStudyHub27 Жыл бұрын
I have a doubt. If there is an array of unknown values then how to find out the min value?
@rishabhgupta1222
@rishabhgupta1222 6 ай бұрын
Understood DP Striver Sir
@chetanthakral5322
@chetanthakral5322 2 жыл бұрын
The problem link in description is leading to some other problem.
@takeUforward
@takeUforward 2 жыл бұрын
Let m get that corrected
@Shivi32590
@Shivi32590 4 ай бұрын
Understood!
@HARSHA_27
@HARSHA_27 11 ай бұрын
Understood!!🙇‍♂
@sumdeb1987
@sumdeb1987 2 жыл бұрын
what is the white board drawing app you are using?
@SaifAliKhan-nm4em
@SaifAliKhan-nm4em 2 жыл бұрын
I did this same question with dp without knowing I was doing dp 😅.
@siyangaming3242
@siyangaming3242 2 жыл бұрын
same with me bro . with same time complexity O(N) and O(1)
@iamnoob7593
@iamnoob7593 9 ай бұрын
US striver
@asikakhatoon3091
@asikakhatoon3091 Жыл бұрын
Time loss.... How we'll be assume which day it'll be 1 and which day it'll be 6
@shashankdaksh7554
@shashankdaksh7554 2 жыл бұрын
Can u please provide solution for tiling with dominoes. I have searched whole KZbin and web but there is no good explanation
@rutikabhuimbar4734
@rutikabhuimbar4734 2 жыл бұрын
Thank you so much for making such amazing content🙌❤
@thisguyispeculiar
@thisguyispeculiar 7 ай бұрын
Understood.
@HassanAbbas-wy7wj
@HassanAbbas-wy7wj Ай бұрын
goat striver💪
@fmkhandwala39
@fmkhandwala39 Жыл бұрын
understood
@thefourhourtalk
@thefourhourtalk 4 ай бұрын
Can anyone please reply Will the sorting technique work here Will sort the entire array And by difference out between the starting and the last index and of course, the difference would be maximum and will return the difference???????????/
@tbcreations2033
@tbcreations2033 4 ай бұрын
No, it will not work.As it will destroy the sequence.
@thefourhourtalk
@thefourhourtalk 4 ай бұрын
@@tbcreations2033 okay sir thanks so much for replying
@itsme9877
@itsme9877 8 ай бұрын
Hello.. Why is it price[i] - mini?????
@parthh3963
@parthh3963 Ай бұрын
in this question what if we need to print the days where we bought and where we sold the stock? can someone give me the solution to this?
@saketpatel8155
@saketpatel8155 2 жыл бұрын
I was waiting for "whenever your heart is broken" and it never came XD
@kunalbandooni4009
@kunalbandooni4009 2 жыл бұрын
Sir, there was no song in the end of this video :(
@THEAMITESHRANJAN
@THEAMITESHRANJAN 16 күн бұрын
"us"
DP 36. Buy and Sell Stock - II | Recursion to Space Optimisation
35:34
take U forward
Рет қаралды 240 М.
Rearrange Array Elements by Sign | 2 Varieties of same Problem
21:37
take U forward
Рет қаралды 215 М.
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 35 МЛН
Хасанның өзі эфирге шықты! “Қылмыстық топқа қатысым жоқ” дейді. Талғарда не болды? Халық сене ме?
09:25
Демократиялы Қазақстан / Демократический Казахстан
Рет қаралды 347 М.
哈哈大家为了进去也是想尽办法!#火影忍者 #佐助 #家庭
00:33
火影忍者一家
Рет қаралды 130 МЛН
How to whistle ?? 😱😱
00:31
Tibo InShape
Рет қаралды 17 МЛН
Next Permutation - Intuition in Detail 🔥 | Brute to Optimal
28:15
take U forward
Рет қаралды 430 М.
Time and Space Complexity - Strivers A2Z DSA Course
35:16
take U forward
Рет қаралды 651 М.
Google Data Center 360° Tour
8:29
Google Cloud Tech
Рет қаралды 5 МЛН
Watch this before you start Coding! | 10 Tips for Coders
22:31
Apna College
Рет қаралды 2,2 МЛН
Best Time to Buy and Sell a Stock II - Leetcode 122 - Python
6:34
My Daughter's Dumplings Are Filled With Coins #funny #cute #comedy
00:18
Funny daughter's daily life
Рет қаралды 35 МЛН