Comic: Find the best time to buy and sell stocks

----- the next day -----

What does that mean? Let's take an example, given the following array:

The stock rise and fall curves corresponding to this array are as follows:

Obviously, buying from when the price is 1 on the 2nd day, and selling from when the price is 8 on the 5th day, you can get the maximum benefit:

The maximum profit at this time is 8-1=7.

In the above example, the maximum value 9 is before the minimum value 1. How should we trade? Can't let time go back, right?

————————————


Take the following figure as an example. If we have determined that the price of 4 is the selling time, which is the best time to buy at this time?

We have to choose the interval before price 4, and it must be the minimum value in the interval. Obviously, the best choice is the time point of price 2.

The first step is to initialize the operation, and use the first element of the array as the temporary minimum price; the initial value of the maximum profit is 0:

The second step is to traverse to the second element. Since 2<9, the current minimum price becomes 2; there is no need to calculate the difference (because the previous element is larger than it), and the current maximum profit is still Is 0:

The third step is to traverse to the third element. Since 7>2, the current minimum price is still 2. As we just said, assuming price 7 is the selling point, the corresponding best buying point is price 2. The difference between the two is 7-2=5, 5>0, so the current maximum profit becomes 5:

The fourth step is to traverse to the fourth element. Since 4>2, the current minimum price is still 2; 4-2=2, 2<5, so the current maximum profit is still 5.

Step 5 , traverse to the fifth element. Since 3>2, the current minimum price is still 2; 3-2=1, 1<5, so the current maximum profit is still 5:

By analogy, we have traversed to the end of the array, the minimum price at this time is 1; the maximum profit is 8-1=7:

public class StockProfit {

    public static int maxProfitFor1Time(int prices[]) {
        if(prices==null || prices.length==0) {
            return 0;
        }
        int minPrice = prices[0];
        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] < minPrice) {
                minPrice = prices[i];
            } else if(prices[i] - minPrice > maxProfit){
                maxProfit = prices[i] - minPrice;
            }
        }
        return maxProfit;
    }

    public static void main(String[] args) {
        int[] prices = {9,2,7,4,3,1,8,4};
        System.out.println(maxProfitFor1Time(prices));
    }

}

Let's take the following array as an example to visually look at the timing of buying and selling:

In the figure, the green line segment represents the stage of price increase. Since there is no limit to the number of transactions, we can buy at every low point and sell at every high point.

In this way, all the green parts are our income, and the maximum total income is the sum of these partial incomes:

(6-1)+(8-3)+(4-2)+(7-4) = 15

As for how to judge these green rising stages? Very simple, we traverse the entire array, but whenever the next element is greater than the previous element, it means that the stock price is rising.

    public int maxProfitForAnyTime(int[] prices) {
        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i] > prices[i-1])
                maxProfit += prices[i] - prices[i-1];
        }
        return maxProfit;
    }

—————END—————

Friends who like this article, welcome to follow the official account  programmer Xiaohui , and watch more exciting content

点个[在看],是对小灰最大的支持!

Guess you like

Origin blog.csdn.net/bjweimengshu/article/details/109302108