Best time to buy and sell stock to Maximise Profit Leetcode - 121 | Java and C++ | DSAOne Course #14
HTML-код
- Опубликовано: 9 мар 2021
- Hey guys, In this video, we're going to solve a very famous Leetcode problem known as the Best time to Buy and Sell a stock - part 1.
Practice here: practice.geeksforgeeks.org/
🚀 Follow me on:
Instagram: / anuj.kumar.sharma
Linkedin: / sharma-kumar-anuj
Twitter: / realanujbhaiya
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
🥳 Join our Telegram Community:
Telegram channel: t.me/coding_enthusiasts
Telegram group: t.me/dsa_one
💸 Visit www.educative.io/anuj to avail discount on all courses on Educative!
💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!
📚 Complete DSA Playlist: • DSA-One Course - The C...
Complete Android Development Playlist: • Android Development Tu...
Hashtags:
#anujbhaiya #dsaone #leetcode
Ignore these tags:
best time to buy and sell stock leetcode
best time to buy and sell stock
stock buy and sell
anuj bhaiya
stock buy and sell leetcode
maximum profit by buying and selling a share at most twice
buy and sell stocks leetcode
121. best time to buy and sell stock
stock buy sell to maximize profit
stock buy and sell problem
leetcode 121
stock buy and sell gfg
leetcode
stock span problem
buy and sell stock leetcode
maximum profit
best time to buy stocks
best time to buy and sell stock ii
when to buy and sell stocks
buy and sell stocks
best time to buy stocks leetcode
stock buy and sell problem leetcode
best time to buy and sell stock iii
best time to sell stocks
leetcode best time to buy and sell stock
buy and sell stock
stock buy sell problem
dsa
when buy and sell stock
dsa one
buy and sell stocks for beginners
maximum profit gfg
best time to buy and sell stock i
kadane's algorithm
stock buy sell leetcode
best time to buy and sell stocks
buying and selling stocks
best time to sell stocks leetcode
leetcode problems
maximum profit by buying and selling a share at most k times
best time to buy and sell stock with transaction fee
kadane algorithm
minimize the heights ii
stock maximize
anuj
buy and sell
maximum profit by buying and selling a share atmost twice
rearrange array in alternating positive & negative items with o(1) extra space
best time to buy and sell stock iv
buy sell stocks
dsa full course
dsa using c++
leet code
love babbar
stock buy sell
stock for profit
stock maximize hackerrank
stock profit
stock span
when to sell stocks
a company has a number of products in stock coding question
algorithms
anuj bhaiya dsa
best time to buy and sell stock leetcode python
best time to buy and sell stock python
buying and selling stocks for beginners
count inversion
dsa with java
dynamic programming problems
garry sandhu
get saged
largest sum contiguous subarray
leetcode solutions
maximum product subarray
maximum profit by buying and selling share at most twice
rain water tapping problem
rain water trapping problem
stock buy
tcs buy or sell
trapping rain water problem
two sum leetcode c++ solution
when share buy and sell
121
algorithm
allocate minimum number of pages
amazon online assessment test
amazon salary
anuj bhaiya java
apna college
apni kaksha python
array program in java
best time to buy and sell stock atmost b times
best time to buy and sell stock ii leetcode
best time to buy and sell stock with cooldown
binary tree maximum path sum
buy sell
buy sell stock leetcode
leetcode
leetcode 121
best time to buy and sell stock leetcode
leet code 121
leetcode 121 best time to buy and sell stock
leetcode 121 python
leetcode 121 solution
leetcode 121 tutorial
eric programming leetcode 121
leetcode solutions
buy and sell stocks leetcode
leetcode 2021
121. best time to buy and sell stock leetcode
leet code 121 explained
leetcode best time to buy and sell stock
python leetcode
leetcode stocks
leetcode solution
leetcode problems
I was asked to solve this problem in Walmart interview.
excellent, teachings and topics and anyone can understand them very easily even though if they don't know the java or c, because he explains the logic in such a way that you can code it it in your preferred language.
Thanks Anuj Bhaiya . Actually I got the logic exact when he told to think how to solve with O(n) and constant space.
Please upload videos more frequently for this course.
Really feeling the love for DSA by your video.. Thank so much @Anuj Bhaiya
method4(int a[]){
int sum=0, maxProfit=0;
for(int i=0; imaxProfit) maxProfit = sum;
if(sum
not working for me
The best thing about your lecture bhaiya is your time complexity(video duration) rest other youtubers take half an hour minimum for same quality.
The way you articulate it's amazing thanks 😃
The explanation becomes more easy to understand when its in hindi, Thanks for the solution.:)
bhai shii me lol! English me utna thik nhi lagta
Interviewer ko bhi yahi bolega kya? How do you expect to explain something in english if you can't understand it in english.
awesome solution, really loved the thought process for each step
Very well explaination starting from brute force approach.
i am so happy that god snd me to code at that time where tech is giving a lot of support in my coding journey , from these smart teachers , i can do anything ...
love you brother! and thanks much for the clear cut explanation.
Very simple and best learning approach
Thank bhaya, just I needing this type of video.
Seriously your Auxiliary array explanation was awesome !! thank you again ...
Thanks for explaining how to start from naive approach to optimized solutions
Bhaiya knows that we don't like long videos, thus he makes parts 😎😎
Please upload the video frequently and more complex question frequently asked in competitive programming
Thanks bhaiya for amazing explaination. ❤️❤️
Love u bro 🤗 Big fan of ur teaching 😊❤️
your explanations are always outstanding, thank you
mind- blowing explanation anuj bhaiya love from Andhra Pradesh
ek dam clear samajhaya bhaiya aapne:)
Outstanding explanation !!!
Great Sir 😎
#TheAnalysers
bhaiya mtlb gazab bahut mast explain kiya
great teaching skills..
Thank u very much, It was great video
Really helpful explanation!
Great explanation 👍
Bhaiya plz second wala bhi video banadena asap if possible u are really doing a great job , thank you so much .
Best Explaination Ever!!
Excellent video👌
Bhaiya one small doubt can Open source Contribution is counted as work experience??
Great Explanation.
Thanks for easy explanation
beautiful approach
Bhaiya I have a request...please make a video on the topic :
Minimum no of jumps to reach end of the array
Brute force is also a solution. Bilkul sahi
Thanks bhaiya it helped a lot
Thank you for explaining
Nice explanation. 👍
Great content
Bhaiya please upload videos more frequently for this course. 🥺
you are my Guardian angel😇😇
another approach for super optimised solution.
class Solution {
public:
int maxProfit(vector& prices) {
int n=prices.size();
int max=prices[n-1],ans=0;
for(int i=n-2;i>=0;i--){
max=prices[i]>max?prices[i]:max;
int p=max-prices[i];
if(p>ans){
ans=p;
}
}
return ans;
}
};
great explanation
Please make a video on the problem of Minimum no of Jumps to reach end of an array
Good lecture 💥
Loved this
Thank you Bhaiya 🥳...
Great logic ,
Thank you Bhaiya
thank you bhaiya
good explanation!!!!
good Approach
Thanks bhaiya!!
Aagaya aagaya maal aagaya😂
thank you so much bhaiya🤗🤗
Thanks!
python solution
def buyAndSellStocks(arr):
maximum = max(arr)
minimum = min(arr[:arr.index(maximum)])
return maximum-minimum
l = list(map(int, input().split()))
print(buyAndSellStocks(l))
This one with O(N) time complexity for returning the days . This is in python.
def buyAndSellStocks(arr) :
Max=-1
for i in range (len(arr)):
mini = arr[i]
maxi= max(arr[ i+1:])
Pr= maxi-mini
if Max
Your program fail with this test case
8,1,7 @Siddhant Goyal
Thanks sir..♥
Amazing
Bhaiyya isko O(N) TC m aur constant space m ek method se aur solve kr skte na ....ek for loop m phle max element nikal lo aur fir ek doosra loop chala k us index tak min element nikalne ka loop chala do....aur un dono ka difference hi max profit hoga.
Excellent teaching given by you which is in very simplest way... Thank you bhaiyaa...
bro at what time you started coding
@@d.aditya1398 right now
Please continue this video series
awsm🤗🤗🤗
thanks bhaiya
Bhaiya isme ek ar trick use kar sakte h kya jese if(a[I]
meanwhile....me reading the question partially and thinking of sorting the array and returning the difference of first and last element🤣🤣🤣🤣🤣🤣
thank you bhaiya ❤️❣️
Anuj Bhaiya hmain sirf profit ni nikalna minsofar bhi btana hai is case main 2 hai but second last element update ho jata hai minsofar = 1
Thanks Auj Bhaiya !
Please upload detailed course for python 🙏
OP bhai
nice question
agar koi solve karne ke baad aaya hai (means code ko optimize karne ke liye)
yaar video dekhne se pehle humne kese solve kiya or koi hmara code dekhega to heart attack aa jayega
fir video dekhne ke baad me 300 line ke code ko 3 line me likhte hai leetcode pe 🤣🤣🤣🤣🤣
Thank You anuj bhaiya.....
DSA-ONE course helps a lot.....
Stay home (if possible)
Stay Safe (must)
Bhaiya
Phale yaadi aapan minimum value ka index nikal le then usse index k baad konsi value max hai vo dekh le toh bhi solve easily ho sakta hai
Complexity mein thodi variation hogi shyd
Waise I love your content BhaiYa. All the Best 👍❤️👍
please bring video on greedy with complete detail
Thanks
Make a video on a.i. development 🔥 road map 🔥
thanks
Sir plz guide me I have 3 years gap after graduation . I want to join a company . I am learning c++ , java , data structures by myself . I am from ece background . What should I do extra like projects , or any specific course so that I can start my career . I have done blunders in my life . I want to support my family . Plz guide me about things that I can do for selection in a company .my friend is in it company I am taking help from him also . Your response is highly appreciated thanks
This one with O(N) time complexity for returning the days . This is in python.
def buyAndSellStocks(arr) :
Max=-1
for i in range (len(arr)):
mini = arr[i]
maxi= max(set(arr[ i: ] ))
Pr= maxi-mini
if Max
Ye amazing course ke liye bohot bohot shukriya ! I hope ki aap aese he detailed videos banate rahenge ...
Ek question aur ek request tha !
Bhaiya , kab tak ye course khatam hoga ?
Aur,
Kya aap ek hafte mein thoda zyada videos daal payenge please ? ( 3-4 )
Innn
First like
Make a video on ai development 🔥
I thought that another tech channel became a fintech channel
Please make a video on rainwater trap
That'll come soon, stay tuned 🙌
@@AnujBhaiya thanks 😀
Need videos on algo trading
catch yahan yahi hai , if lowest price is at the end of the array then, how anyone can maximise profit ;)
bhaiya you teaching in very lucid language and now i think DSA is not much difficult.
what if there is 0 after 8?
any corner case for that?
❤
Bhaiya agr hum 2 loop lele allg allg jisme ek mai min check hora ho aur index store hora ho uska, agle loop mai max check hora h aur index b check hora h taaki min k baad hi aaye max. Tb profit nikaal diya vo b linear time mai hora h. Is this also correct??
Bhaiya this can be further optimised to log(n) time complexity using ordered sets or PBDS in C++ GNU. 🙏 Would be helpful if you make a video on it.
Really? how?
Lol..to construct the data structure itself you would require O(NlogN) time, but you can query in O(logN)
plz make video more frequently
Bhaiya urban company mai job k liye kya skills required hoti hai
By what time entire course of DSA be uploaded
Anuj bhaiya you saved Anuj bhaiya, but i am having some issues with a similar problem just with multiple stocks given. Can you help on how this can be adapted for that problem also at most k transactions are allowed.
Bhaiya bacha lo
First comment ❤️♥️