DP 35. Best Time to Buy and Sell Stock | DP on Stocks 🔥
Vložit
- čas přidán 14. 03. 2022
- Lecture Notes/C++/Java Codes: takeuforward.org/dynamic-prog...
Problem Link: bit.ly/3rN7GIL
Please watch the video at 1.25x for a better experience.
Pre-req for this Series: • Re 1. Introduction to ...
a
Make sure to join our telegram group for discussions: linktr.ee/takeUforward
Full Playlist: • Striver's Dynamic Prog...
In this video, we solve the first problem on DP on Stocks. Its the Best time to buy and sell stocks.
If you have not yet checked our SDE sheet, you should definitely do it: takeuforward.org/interviews/s...
You can also get in touch with me at my social handles: linktr.ee/takeUforward
Please give us a like and share this content as much as possible :)
Thank you bro for your hard work to add new videos. Congratulations for completing 2 years on youtube :)
When will remaining DP problems will come ?
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.
true.. it greedy works best for this question
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
Yes, I agree with you
Because of this guy, I love dynamic programming.
Yes u will until u start solving different questions
@@rahulbhagat4023 so these questions are not enough?
@@rahulbhagat4023 Everyone starts somewhere ig. Many people found these problems ridiculously hard earlier
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)
@@nanda_8 yha 1500 nhi hore h bhai tu 1800 ki baat kr ra hai
maan gye guru! this was my first video here and explnation was very good!
UNDERSTOOD.....Thank You So Much for this wonderful video.....🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
I think u are the best teacher in this whole world...keep it up man...
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.
Understood. Thankyou Sir
Understood. Thanks a lot. Please upload more videos Bhaiyaaa
Nobody explains like you. Awesome.
There are many ways of solving this problem, but your one is the best.!
Following your entire DSA A2Z Course!
Thank You!
Understood
Understood! thanks !! Long live !!!
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.
Thank you bro. Understood
good stuff buddy
understood , thanks
Understood Sir!
Man, your DP playlist on CZcams will gonna rock Man....!!!!! 🥳
Understood✅🔥🔥
Mast understood !!!!!!
Ur really a saver of my life bhayya
UNDERSTOOD!
Understood ❤
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 ❤
I want to know how did you become a pure developer without DSA?
@@yashrajdeshmukh6759 U just need to know basics of DSA , OOPs Thats it .
understood. Please make a playlist for Greedy too. Thank You.
Understood 💗
Thank you so much for making such amazing content🙌❤
understood sir !
'
understood💙
understood everything
Understood!!🙇♂
understood ❤
On the A2Z course u have this ques under arrays so how are we supposed to do it without dp?
Awesome Sir..................
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?
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 💗
Understood !! :)
Understood!
Wondering, if you should include this in DP playlist
how would be able to solve it using recurrence. I'm unable to figure it out
understood
Understood !!
can anyone explain for me why the space complexity is O(1) like he said?
Understood.
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
best DP series ever....thanks Striver 💛
I have a doubt. If there is an array of unknown values then how to find out the min value?
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.
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.
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
Understood
Can u please provide solution for tiling with dominoes. I have searched whole CZcams and web but there is no good explanation
question link given in the desc. box is wrong
Understood:)
Hello.. Why is it price[i] - mini?????
By the way Engineering means optimisation
so we have to optimise things
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
Sir can you please cover Linked List like Array series.Within a month ??
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.
are you there yet buddy?
what is the white board drawing app you are using?
Amazing explaination :)
How is Dynamic Programming ?
Why the lecturers never teach like this during college🙁
magician
🔥🔥🔥🔥🔥🔥
Why u didn’t do it with recursive dp?
badiya
Understood boss\/
Understood DP Striver Sir
I was waiting for "whenever your heart is broken" and it never came XD
SDE Sheet Day 1 Problem 6 Done!
We also apply that first find min from array then from that index to end find the maximum element,
please reply @take U Forward
Yeah even I got the same idea
fails for this case 2 7 1 2. answer is 5 (7-2).
never knew that we called this also as dynamic programming 😅
Understood. Thank you so much
was waiting for this one......
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???????????/
No, it will not work.As it will destroy the sequence.
@@tbcreations2033 okay sir thanks so much for replying
4:00 i will approach this with priority queue
US striver
Sir, there was no song in the end of this video :(
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 ...
The problem link in description is leading to some other problem.
Let m get that corrected
Damn well understood!!
When doing the problem without knowing that it's dp feels so easy.
Understood :)
Done myself earlier itself
Aug'1,2023 10:10am
❤❤
man you are amazing
how are you able to think like this
I did this same question with dp without knowing I was doing dp 😅.
same with me bro . with same time complexity O(N) and O(1)
bhaiya please make a set of questions so that we can easily solve leetcode contest questions
Time loss.... How we'll be assume which day it'll be 1 and which day it'll be 6
21
this code is not accepting anymore
Can anybody tell me why we have to keep track of minimum cost too
to buy it at the lowest cost
"understood".
US
understood.!!!!!!!!!!!!!!!
How to do with memoization?
🤣
yes
Thanks striver ❣️
Thank you, bhaiya!!
this solution is wrong for the test case, price = [100,180,260,310,40,535,695]