Algorithm Day 2: The Best Time to Buy and Sell Stocks

LeetCode question 122:

Define an array prices, where prices[i] is the price of a given stock on day i.

Design an algorithm to calculate the maximum profit you can make. You can complete as many trades as possible (buy and sell a stock multiple times).

Note: You cannot participate in multiple transactions at the same time (you must sell the previous stock before buying again).

Example 1:

Input: prices = [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (stock price = 1), sell on day 3 (stock price = 5) Out, this transaction can gain profit = 5-1 = 4.
Then, buy on the 4th day (stock price = 3) and sell on the 5th day (stock price = 6), this transaction can make a profit = 6-3 = 3.

Example 2:

Input: prices = [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (stock price = 1), sell on day 5 (stock price = 5), This transaction can make a profit = 5-1 = 4.
Note that you cannot buy shares on Day 1 and Day 2 back-to-back and sell them later. Because this is involved in multiple transactions at the same time, you must sell the previous shares before buying again.

Example 3:

Input: prices = [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is completed, so the maximum profit is 0

Tips :

1 <= prices.length <= 3 * 104
0 <= prices[i] <= 104

Ideas:

  • The answer that needs to be obtained is: the maximum profit, the acquisition of profit consists of two parts, buying stocks and selling stocks, and the net profit of buying stocks and selling stocks gets the final answer. The buying and selling of stocks is related to the pricing of stocks every day.

  • Atomizing the stock transactions of a certain two days can be concluded that in order to finally generate profits, the stock price of the next day must be higher than that of the previous day. We can recognize that this transaction can be carried out as long as the transaction profits of the adjacent two days are positive, and add the transaction profits that meet this condition in all days to get the optimal solution. (Greedy algorithm) ****, that is, the local optimal solution leads to the overall optimal solution .
    Note: The optimal solution obtained by the greedy algorithm is not necessarily the real optimal solution of this problem. This process requires mathematical proof to solve. How does this problem reflect the local optimum? When the stock trading profits of two adjacent days are positive, Then we think that this process is optimal, and the transactions of any adjacent two days in this question are isolated and do not affect each other, which meets the satisfaction conditions of the greedy algorithm.

    Code:

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

The above only represents the author's personal opinion. If there is any mistake, please forgive me and point it out to the author in time.

Guess you like

Origin blog.csdn.net/qq_52696089/article/details/120469678