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

Комментарии • 199

  • @ApurvKhare
    @ApurvKhare 3 года назад +112

    I was asked to solve this problem in Walmart interview.

  • @dharmateja7315
    @dharmateja7315 2 года назад +7

    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.

  • @aritralahiri8321
    @aritralahiri8321 3 года назад +6

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

  • @mohammedmustufa9928
    @mohammedmustufa9928 3 года назад +63

    Please upload videos more frequently for this course.

  • @abidfarooq2845
    @abidfarooq2845 2 года назад +2

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

  • @sreyassubramanian2125
    @sreyassubramanian2125 3 года назад +12

    method4(int a[]){
    int sum=0, maxProfit=0;
    for(int i=0; imaxProfit) maxProfit = sum;
    if(sum

  • @shreshthjha997
    @shreshthjha997 5 месяцев назад +3

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

  • @ankitkholiya3753
    @ankitkholiya3753 Год назад +2

    The way you articulate it's amazing thanks 😃

  • @jitendrasahu7007
    @jitendrasahu7007 2 года назад +20

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

    • @Pamir026
      @Pamir026 2 года назад +3

      bhai shii me lol! English me utna thik nhi lagta

    • @_sayandas
      @_sayandas 11 месяцев назад

      Interviewer ko bhi yahi bolega kya? How do you expect to explain something in english if you can't understand it in english.

  • @pragnyatata491
    @pragnyatata491 10 месяцев назад

    awesome solution, really loved the thought process for each step

  • @akankshakumari213
    @akankshakumari213 2 года назад

    Very well explaination starting from brute force approach.

  • @toshig7998
    @toshig7998 Год назад +1

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

  • @RavinderSingh-yo7dx
    @RavinderSingh-yo7dx 2 года назад

    love you brother! and thanks much for the clear cut explanation.

  • @javeedshariff2797
    @javeedshariff2797 2 года назад

    Very simple and best learning approach

  • @nityabehera5003
    @nityabehera5003 3 года назад +2

    Thank bhaya, just I needing this type of video.

  • @rohithgorthy971
    @rohithgorthy971 3 месяца назад

    Seriously your Auxiliary array explanation was awesome !! thank you again ...

  • @sulemankhan523
    @sulemankhan523 Год назад

    Thanks for explaining how to start from naive approach to optimized solutions

  • @parthkabra4968
    @parthkabra4968 3 года назад +1

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

  • @hashcode4325
    @hashcode4325 3 года назад +2

    Please upload the video frequently and more complex question frequently asked in competitive programming

  • @abhinavdeshmukh812
    @abhinavdeshmukh812 3 года назад

    Thanks bhaiya for amazing explaination. ❤️❤️

  • @anshumanlenka1261
    @anshumanlenka1261 3 года назад

    Love u bro 🤗 Big fan of ur teaching 😊❤️

  • @prashantsharma4290
    @prashantsharma4290 2 года назад +12

    your explanations are always outstanding, thank you

  • @durgamjuber1108
    @durgamjuber1108 Год назад +1

    mind- blowing explanation anuj bhaiya love from Andhra Pradesh

  • @himanshuvaishya9164
    @himanshuvaishya9164 2 года назад

    ek dam clear samajhaya bhaiya aapne:)

  • @ashishkabi4347
    @ashishkabi4347 2 года назад

    Outstanding explanation !!!

  • @rakeshkumarnahak5320
    @rakeshkumarnahak5320 3 года назад +14

    Great Sir 😎
    #TheAnalysers

  • @doremon81072
    @doremon81072 Год назад

    bhaiya mtlb gazab bahut mast explain kiya

  • @deepakyadav-yu9qn
    @deepakyadav-yu9qn 3 года назад +1

    great teaching skills..

  • @piyushgaur6975
    @piyushgaur6975 2 года назад

    Thank u very much, It was great video

  • @algogirl3261
    @algogirl3261 2 года назад

    Really helpful explanation!

  • @akashdey7881
    @akashdey7881 3 года назад +2

    Great explanation 👍

  • @pranavtanpure8131
    @pranavtanpure8131 3 года назад +1

    Bhaiya plz second wala bhi video banadena asap if possible u are really doing a great job , thank you so much .

  • @abhaygaikwad1532
    @abhaygaikwad1532 8 месяцев назад

    Best Explaination Ever!!

  • @chiragmehta1556
    @chiragmehta1556 3 года назад +4

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

  • @sgrpnwr
    @sgrpnwr 2 года назад

    Great Explanation.

  • @i.am.gaurav.khanna
    @i.am.gaurav.khanna 2 года назад

    Thanks for easy explanation

  • @yuktisharma5029
    @yuktisharma5029 Год назад

    beautiful approach

  • @selflearner8895
    @selflearner8895 3 года назад +2

    Bhaiya I have a request...please make a video on the topic :
    Minimum no of jumps to reach end of the array

  • @manishsharma2211
    @manishsharma2211 3 года назад +1

    Brute force is also a solution. Bilkul sahi

  • @akashchawlamusic
    @akashchawlamusic Год назад

    Thanks bhaiya it helped a lot

  • @girish9841
    @girish9841 7 месяцев назад

    Thank you for explaining

  • @shishirkashyap9492
    @shishirkashyap9492 Год назад

    Nice explanation. 👍

  • @vachhanijeel2080
    @vachhanijeel2080 3 года назад

    Great content

  • @Ankur-zd4db
    @Ankur-zd4db 3 года назад +1

    Bhaiya please upload videos more frequently for this course. 🥺

  • @arham_aziz
    @arham_aziz 3 года назад

    you are my Guardian angel😇😇

  • @rollercoaster9719
    @rollercoaster9719 Год назад

    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;
    }
    };

  • @subhadeepghosh2813
    @subhadeepghosh2813 3 года назад

    great explanation

  • @mollamainuddin6485
    @mollamainuddin6485 3 года назад

    Please make a video on the problem of Minimum no of Jumps to reach end of an array

  • @shyamkumar2606
    @shyamkumar2606 3 года назад

    Good lecture 💥

  • @yasirhussain1875
    @yasirhussain1875 2 года назад

    Loved this

  • @SaumyaSharma007
    @SaumyaSharma007 2 года назад +1

    Thank you Bhaiya 🥳...

  • @gudlagudla7104
    @gudlagudla7104 Год назад

    Great logic ,

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l 5 месяцев назад

    Thank you Bhaiya

  • @nashimbiswakarma
    @nashimbiswakarma Год назад +1

    thank you bhaiya

  • @saritaprasad4295
    @saritaprasad4295 Год назад

    good explanation!!!!

  • @abhaytiwari6411
    @abhaytiwari6411 3 года назад

    good Approach

  • @prakharagarwal6237
    @prakharagarwal6237 3 года назад

    Thanks bhaiya!!

  • @abhishekkumarsingh8603
    @abhishekkumarsingh8603 3 года назад +13

    Aagaya aagaya maal aagaya😂

  • @aviralkaushik9425
    @aviralkaushik9425 2 года назад

    thank you so much bhaiya🤗🤗

  • @akshaygoyal4118
    @akshaygoyal4118 3 года назад +2

    Thanks!

  • @siddhantgoyal25
    @siddhantgoyal25 3 года назад +5

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

    • @kingmaker9082
      @kingmaker9082 2 года назад

      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

    • @karanmandveb-5818
      @karanmandveb-5818 Год назад

      Your program fail with this test case
      8,1,7 @Siddhant Goyal

  • @TheOneFromEarth
    @TheOneFromEarth Год назад

    Thanks sir..♥

  • @Anonymous-coder31
    @Anonymous-coder31 3 года назад

    Amazing

  • @utkarshjain9569
    @utkarshjain9569 2 года назад

    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.

  • @sanketrajnor3096
    @sanketrajnor3096 11 месяцев назад

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

    • @d.aditya1398
      @d.aditya1398 11 месяцев назад

      bro at what time you started coding

    • @sanketrajnor3096
      @sanketrajnor3096 11 месяцев назад

      @@d.aditya1398 right now

  • @sravanikolluru8129
    @sravanikolluru8129 2 года назад +1

    Please continue this video series

  • @milimishra6447
    @milimishra6447 3 года назад +1

    awsm🤗🤗🤗

  • @badal8435
    @badal8435 2 года назад

    thanks bhaiya

  • @gauravkumar0777
    @gauravkumar0777 3 года назад

    Bhaiya isme ek ar trick use kar sakte h kya jese if(a[I]

  • @097sreesivaranjini7
    @097sreesivaranjini7 Год назад +2

    meanwhile....me reading the question partially and thinking of sorting the array and returning the difference of first and last element🤣🤣🤣🤣🤣🤣

  • @lokendrasingh9780
    @lokendrasingh9780 2 года назад

    thank you bhaiya ❤️❣️

  • @waqarahmed5408
    @waqarahmed5408 3 года назад +1

    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

  • @RudraprasadMohapatraa
    @RudraprasadMohapatraa Год назад +1

    Thanks Auj Bhaiya !

  • @_obito9
    @_obito9 3 года назад

    Please upload detailed course for python 🙏

  • @proindies
    @proindies 3 года назад

    OP bhai

  • @mridultailor6510
    @mridultailor6510 3 года назад +1

    nice question

  • @harshshiyani1011
    @harshshiyani1011 3 года назад

    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)

  • @anirudhgoutam6401
    @anirudhgoutam6401 3 года назад

    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 👍❤️👍

  • @rajshekharrakshit9058
    @rajshekharrakshit9058 3 года назад

    please bring video on greedy with complete detail

  • @kunalbahirat7795
    @kunalbahirat7795 2 года назад

    Thanks

  • @brijenpatel4564
    @brijenpatel4564 3 года назад +1

    Make a video on a.i. development 🔥 road map 🔥

  • @arshdeep011
    @arshdeep011 Год назад

    thanks

  • @saurabh2225
    @saurabh2225 3 года назад +6

    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

  • @kingmaker9082
    @kingmaker9082 2 года назад

    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

  • @budhadityachatterjee6270
    @budhadityachatterjee6270 3 года назад

    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 )

  • @brijenpatel4564
    @brijenpatel4564 3 года назад +6

    First like

  • @adityasingla3148
    @adityasingla3148 3 года назад +4

    I thought that another tech channel became a fintech channel

  • @aryansaxena4630
    @aryansaxena4630 3 года назад +5

    Please make a video on rainwater trap

  • @e-startupideas8849
    @e-startupideas8849 3 года назад +2

    Need videos on algo trading

  • @nimishshrivastava6709
    @nimishshrivastava6709 2 года назад +1

    catch yahan yahi hai , if lowest price is at the end of the array then, how anyone can maximise profit ;)

  • @Yash_Parashar
    @Yash_Parashar 3 года назад +1

    bhaiya you teaching in very lucid language and now i think DSA is not much difficult.

  • @rajchandra6913
    @rajchandra6913 Год назад +1

    what if there is 0 after 8?
    any corner case for that?

  • @Krishna-le2vz
    @Krishna-le2vz 3 года назад

  • @gautamnegi6868
    @gautamnegi6868 3 года назад

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

  • @snehilsinha5624
    @snehilsinha5624 3 года назад +4

    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.

    • @samyakbakliwal2914
      @samyakbakliwal2914 3 года назад

      Really? how?

    • @atharvadere5709
      @atharvadere5709 2 года назад +5

      Lol..to construct the data structure itself you would require O(NlogN) time, but you can query in O(logN)

  • @subhadeepghosh2813
    @subhadeepghosh2813 3 года назад

    plz make video more frequently

  • @ankusharora5551
    @ankusharora5551 3 года назад

    Bhaiya urban company mai job k liye kya skills required hoti hai

  • @tech_wizard9315
    @tech_wizard9315 3 года назад +3

    By what time entire course of DSA be uploaded

  • @anujbhardwaj6411
    @anujbhardwaj6411 Год назад

    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

  • @Hariom-dj6vl
    @Hariom-dj6vl 3 года назад +1

    First comment ❤️♥️