Best time to buy and sell stock to Maximise Profit Leetcode - 121 | Java and C++ | DSAOne Course #14

preview_player
Показать описание
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.

🚀 Follow me on:

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝

🥳 Join our Telegram Community:

💸 Use coupon code ANUJBHAIYA on GeeksforGeeks to avail discounts on courses!

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
Рекомендации по теме
Комментарии
Автор

The explanation becomes more easy to understand when its in hindi, Thanks for the solution.:)

jitendrasahu
Автор

method4(int a[]){
int sum=0, maxProfit=0;
for(int i=0; i<a.length-1; i++){
sum+=a[i+1]-a[i];
if(sum>maxProfit) maxProfit = sum;
if(sum<0) sum=0;
}
return maxProfit; }

Similar to last video (Kadane's algorithm)

sreyassubramanian
Автор

I was asked to solve this problem in Walmart interview.

ApurvKhare
Автор

Please upload videos more frequently for this course.

mohammedmustufa
Автор

The best thing about your lecture bhaiya is your time complexity(video duration) rest other youtubers take half an hour minimum for same quality.

shreshthjha
Автор

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.

dharmateja
Автор

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))

siddhantgoyal
Автор

Thanks Anuj Bhaiya . Actually I got the logic exact when he told to think how to solve with O(n) and constant space.

aritralahiri
Автор

another approach for super optimised solution.


class Solution {
public:
int maxProfit(vector<int>& prices) {
int n=prices.size();
int max=prices[n-1], ans=0;
for(int i=n-2;i>=0;i--){

int p=max-prices[i];
if(p>ans){
ans=p;
}
}
return ans;
}
};

rollercoaster
Автор

Really feeling the love for DSA by your video.. Thank so much @Anuj Bhaiya

abidfarooq
Автор

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 ...

toshig
Автор

Excellent video👌
Bhaiya one small doubt can Open source Contribution is counted as work experience??

chiragmehta
Автор

The way you articulate it's amazing thanks 😃

ankitkholiya
Автор

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

saurabh
Автор

from your solution not a single test case is passing in binary search can u pls tell why??? Testcase

prices = [1, 5, 3, 4, 6]
Your result

5
Expected

7
@Anuj Bhaiya

mohdkhalidkhan
Автор

your explanations are always outstanding, thank you

prashantsharma
Автор

Probably this is better code to understand.

int prices[] = {3, 5, 2, 1, 6, 9, 5, 6};
int MinPrice = prices[0];
int MaxProfit = 0;

for(int i=0;i<prices.length;i++)
{
if(prices[i]<MinPrice) MinPrice=prices[i];


{
MaxProfit = prices[i]-MinPrice;
}
}

chinmay
Автор

mind- blowing explanation anuj bhaiya love from Andhra Pradesh

durgamjuber
Автор

Bhaiya knows that we don't like long videos, thus he makes parts 😎😎

parthkabra
Автор

Excellent teaching given by you which is in very simplest way... Thank you bhaiyaa...

sanketrajnor