Buy and Sell Stocks-I and II

Problem link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/
Twitter | LinkedIn

This is basic array question helps you to understand arrays better

part -1: buying and selling is allowed once

in this we will assume maxi…


This content originally appeared on DEV Community and was authored by Swapnil Gupta

Problem link : https://leetcode.com/problems/best-time-to-buy-and-sell-stock/submissions/
Twitter | LinkedIn

This is basic array question helps you to understand arrays better

part -1: buying and selling is allowed once

in this we will assume maximum profit zero initially and buying price maximum. We will iterate the array of prices, if we find the minimum buyprice less than the previous buying price we will update the buy price, for this we will use if statement if(prices[i]<buyprice>){ buyprice =price(i);} this loop will execute only if price[i] value is smaller than the buyprice, or we can use the java Math.min class of java.

Similarly, we update the maxProfit if our maxProfit is more than price[i]-buyPrice or previous maxProfit. for this either we can use else if statement or MaxProfit= Math.max(MaxProfit, prices[i]-buyPrice);

class Solution {
    public int maxProfit(int[] prices) {
        int MaxProfit=0;
        int buyPrice= Integer.MAX_VALUE;

        for(int i=0; i< prices.length; i++){
            if(prices[i]<buyPrice){
                buyPrice= prices[i];
            }
         // else if(MaxProfit<prices[i]- buyPrice){
         //    MaxProfit= prices[i]-buyPrice;
            //}
            MaxProfit= Math.max(MaxProfit, prices[i]-buyPrice);


    }
        return MaxProfit;
    }
}


This content originally appeared on DEV Community and was authored by Swapnil Gupta


Print Share Comment Cite Upload Translate Updates
APA

Swapnil Gupta | Sciencx (2022-04-07T17:39:33+00:00) Buy and Sell Stocks-I and II. Retrieved from https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/

MLA
" » Buy and Sell Stocks-I and II." Swapnil Gupta | Sciencx - Thursday April 7, 2022, https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/
HARVARD
Swapnil Gupta | Sciencx Thursday April 7, 2022 » Buy and Sell Stocks-I and II., viewed ,<https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/>
VANCOUVER
Swapnil Gupta | Sciencx - » Buy and Sell Stocks-I and II. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/
CHICAGO
" » Buy and Sell Stocks-I and II." Swapnil Gupta | Sciencx - Accessed . https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/
IEEE
" » Buy and Sell Stocks-I and II." Swapnil Gupta | Sciencx [Online]. Available: https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/. [Accessed: ]
rf:citation
» Buy and Sell Stocks-I and II | Swapnil Gupta | Sciencx | https://www.scien.cx/2022/04/07/buy-and-sell-stocks-i-and-ii/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.