Stock Buy Sell to Maximize Profit | GeeksforGeeks

Sdílet
Vložit
  • čas přidán 6. 04. 2017
  • Explanation for the article: www.geeksforgeeks.org/stock-bu...
    This video is contributed by Harshit Jain.
    Read More: www.geeksforgeeks.org/stock-b...

Komentáře • 71

  • @fatmaramadan318
    @fatmaramadan318 Před 4 lety +1

    which algorithm did u use?

  • @pranaychandra8016
    @pranaychandra8016 Před 5 lety +3

    how come the TC be o(n) if you are using while inside another while ?

    • @ValentinBaca
      @ValentinBaca Před 5 lety +5

      Pranay Chandra because the looping var isn't reset within the loop. Each element in the index is still only looked at most once, so still O(n). The while loops just keep the index variable within bounds.

  • @pavanpp4042
    @pavanpp4042 Před 3 lety +2

    Could you tell which type of approach is this algorithm?

    • @muhammadhaseeb4096
      @muhammadhaseeb4096 Před 2 lety +2

      greedy algorithm.. as we are trying to make an optimal decision using local values i.e. not taking into account the whole data or global values

  • @subhamchakraborty6822
    @subhamchakraborty6822 Před 2 lety +2

    Excellent Explanation!♥

  • @wanderlustsiddhi
    @wanderlustsiddhi Před 4 lety +4

    well explained!

  • @prnk139
    @prnk139 Před 4 lety

    Well Explained !!

  • @utpalpodder-pk6vq
    @utpalpodder-pk6vq Před 3 lety +2

    i think here at least 3 elements needed to make the profit...

  • @kunalagarwal5423
    @kunalagarwal5423 Před rokem

    why are we not selling on day 5? for max profit

  • @sunilangadi6709
    @sunilangadi6709 Před 6 lety +3

    one of the geeks article says that size of(arr)/ size of(arr[0]) will not provide no.of elements in that array but you people only using that logic.

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 6 lety +4

      Using size of(arr)/ size of(arr[0]) is not advised inside another function, when array is passed as parameter. Otherwise, it is fine. Check out here: www.geeksforgeeks.org/using-sizof-operator-with-array-paratmeters/

    • @ashutoshaswal
      @ashutoshaswal Před 4 lety +2

      They are just saying not to use that method in another function because if you pass array to a function then we doesn't
      pass the whole array to the function ,we just pass the base address to a pointer . So in that case instead of getting array size you will get the pointer size(which receive the base address) and that will give you an incorrect answer.(except for the case when pointer size and array size is similar)

  • @adityapaithon6499
    @adityapaithon6499 Před 6 lety +2

    Awesome explaination

  • @UserUnknown07
    @UserUnknown07 Před 2 lety +1

    Thanks!

  • @vishalalhade2376
    @vishalalhade2376 Před 3 lety

    Thanks

  • @nirmesh44
    @nirmesh44 Před 4 lety +4

    minima condition is wrong price[i]

    • @vishalmishra1937
      @vishalmishra1937 Před 4 lety +3

      its right trace properly ,it means if current price is more than next one then iterate otherwise stop and it is minima .as much as what i have understood

    • @urbantech28
      @urbantech28 Před 3 lety +2

      while(i=price[i])
      break;
      else i++;
      }
      for your case we have to do like this

  • @ashitpanda2905
    @ashitpanda2905 Před 6 lety +4

    Here in this code, you have meesed up both 2 if conditions. TO find the local minima, the current element should be smaller than the next element.But you wrote (if(iprice[i+1])..where it should be (if(i

    • @UJJAINI22
      @UJJAINI22 Před 5 lety

      // Find Local Minima. Note that the limit is (n-2) as we are
      // comparing present element to the next element.
      while ((i < n-1) && (price[i+1]

    • @jaideepsingh7955
      @jaideepsingh7955 Před 2 lety +1

      he has explained in one way, and done the code in the other...you stick to the one you want

  • @ajourney179
    @ajourney179 Před 5 lety

    good video.

  • @hardiksanghavi2003
    @hardiksanghavi2003 Před 7 lety +1

    In the "Interval" structure instance "sol", why has it been declared to have (n/2+1) elements? shouldn't just (n/2) suffice as buy and sell are always in pairs?

    • @GeeksforGeeksVideos
      @GeeksforGeeksVideos  Před 7 lety

      Buy and Sell are paired into one structure, yes. But, n represents the number of days which maybe odd. Hence, n/2+1.

    • @hardiksanghavi2003
      @hardiksanghavi2003 Před 7 lety

      Sir, even when the "n" is odd (like in the given example as well), it shouldn't matter, the code will still work with n/2 instances of "Interval" structure.

    • @MassiveAchievement
      @MassiveAchievement Před 5 lety +1

      @@GeeksforGeeksVideos why not just do this :
      def max_profit_smart(arr):
      profit = 0
      for i in range(0, len(arr) - 1):
      if arr[i + 1] > arr[i]:
      profit += arr[i + 1] - arr[i]
      // sum of a[i+1] - a[i] as 0

    • @a.yashwanth
      @a.yashwanth Před 4 lety

      @@MassiveAchievement comments in python are written with # not //😜

    • @MassiveAchievement
      @MassiveAchievement Před 4 lety

      @@a.yashwanth I know, but I don't care

  • @abhishektiwari5932
    @abhishektiwari5932 Před 3 lety +12

    Using python is legal too...constitution still allows it

    • @mayankjain9821
      @mayankjain9821 Před 2 lety +2

      Yup, feel the pain for python users trying to learn dsa. Every other explanation is in either Java or Cpp

    • @batfishh
      @batfishh Před 13 dny

      it's literally just a loop, no matter what language he uses, it's the same logic

  • @AnkitPandey-fj5bx
    @AnkitPandey-fj5bx Před 5 měsíci

    Complicated the question way too much while implementation
    just sell the stock whenever it goes up and subtract the previous minima to get the maximum profit
    class Solution {
    public int maxProfit(int[] prices) {
    int min = 0,profit=0;
    for(int i:prices){
    if(min

  • @codestorywithMIK
    @codestorywithMIK Před 7 lety +1

    very helping.

  • @ashwani6360
    @ashwani6360 Před 7 lety +1

    night ppl

  • @vipulsingh4637
    @vipulsingh4637 Před 6 lety +6

    The logic is to sell stock if the prices are falling next day. Then buy again next day and keep doing the same procedure. So if there is no local Maxima, max profit would be 0.

  • @SachinVerma-wn1lg
    @SachinVerma-wn1lg Před 6 lety +2

    How it will for {10, 22, 5, 75, 65, 80}
    Here
    local Minima | 10 | 5 | 65 |
    Local Maxima | 22 | 75 | 80 |
    Now how to choose two pairs?

    • @vipulsingh4637
      @vipulsingh4637 Před 6 lety +3

      We don't have to choose two pairs. We just have to find maximum profit. In this case max profit would be = (22-10) + (75-5) + (80-65)

    • @nitinrathee7077
      @nitinrathee7077 Před 5 lety +2

      @@vipulsingh4637 if we are asked to find just 2(as a constraint) , then sort each on basis of profit and pick the maximum too :)

    • @a.yashwanth
      @a.yashwanth Před 4 lety

      @@nitinrathee7077 yeah. But that takes nlogn

    • @nitinrathee7077
      @nitinrathee7077 Před 4 lety

      @@a.yashwanth take two variables max1 and max2 if you want to avoid sorting

    • @decoygaming3455
      @decoygaming3455 Před 4 lety

      @@nitinrathee7077 [7,1,5,3,6,4] for 1 it should be 5 not 4

  • @swapnilgupta236
    @swapnilgupta236 Před 4 lety +2

    I dont know why no one is pointing.. this program is wrong..
    lets take this arr = {2,16,15,30,8,25,80};
    local minima = 2 | 15 | 8 |
    local maxima = 16 | 30 | 80 |
    max diff out of 14, 15 and 72 is 72..coming as my max profit.
    but if i buy at 2 and sell at 30 then buy at 8 and sell at 80.. max profit will be 100.

    • @saptarshishome4409
      @saptarshishome4409 Před 4 lety

      14+15+72=101 and according to your strategy its 100

    • @omingole7304
      @omingole7304 Před rokem

      net profit is to be maximized, not a particular profit...

  • @abhikkhan7431
    @abhikkhan7431 Před 4 lety

    if the number of transactions are given and there exists n number of local maxima and minima then??????????????

    • @a.yashwanth
      @a.yashwanth Před 4 lety

      How come there are n maximas and minimas.

  • @r0binz0n
    @r0binz0n Před 6 lety +2

    this is just wrong. try 140 instead of 40. {100, 180, 260, 310, 140, 535, 695} - obviously the best strategy is to buy on day 0 and sell on last day.

    • @piyush2994
      @piyush2994 Před 6 lety +1

      (310 - 100) + (695 - 140) = 765
      Maybe you are confusing it with another problem - where it is allowed to only buy and sell once. Here you can buy and sell the stock any no. of times.

    • @r0binz0n
      @r0binz0n Před 6 lety

      i think you are right, but then this is just trivial. this problem becomes challenging only when you are allowed to buy and sell once.

    • @PritamBanerjee999
      @PritamBanerjee999 Před 5 lety +1

      Then it is very easy, you just need to find the maximum and minimum.

    • @soumik76
      @soumik76 Před 5 lety

      What if the max appears before the min? like {600 180 450 220}

    • @bestmovies36
      @bestmovies36 Před 5 lety

      @@soumik76 buy on 2nd day and sell on 3rd day

  • @radhekrishanrathod9001
    @radhekrishanrathod9001 Před 5 lety +1

    WTF! YOU ARE TYPING WHILE SAYING VERY IRRITATING NOICE...

  • @prakashbangaru2991
    @prakashbangaru2991 Před 6 lety +1

    This Solution is wrong for the input {1,2,3,4,5}.

    • @parryismful
      @parryismful Před 5 lety +1

      Profit: 5-1=4 which is maximum.Solution is right.

  • @parasgulati31
    @parasgulati31 Před 4 lety

    stop explanation

  • @luisishere987
    @luisishere987 Před 5 lety +2

    very flawed video

  • @abhisekmishra5245
    @abhisekmishra5245 Před 5 lety +2

    poor explanation

  • @riteshsingh3420
    @riteshsingh3420 Před 6 lety +1

    How about iterating backwards?
    I could solve it with just one loop.
    public class Solution {
    public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    int size = scanner.nextInt();
    for (int i = 0; i < size; i++) {
    int numOfDays = scanner.nextInt();
    int[] prices = new int[numOfDays];
    for (int j = 0; j < numOfDays; j++) {
    prices[j] = scanner.nextInt();
    }
    System.out.println(getMaxProfit(prices));
    }
    }
    public static long getMaxProfit(int[] prices) {
    long profit = 0L;
    int maxSoFar = 0;
    for (int i = prices.length - 1; i > -1 ; i--) {
    if (prices[i] >= maxSoFar) {
    maxSoFar = prices[i];
    }
    profit += maxSoFar - prices[i];
    }
    return profit;
    }
    }

  • @UJJAINI22
    @UJJAINI22 Před 5 lety +3

    very well explained!