#leetCode brush title documentary Day21

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii/

Given an array, which i-th element is a given stock i-day prices.

Design an algorithm to compute the maximum profit you can get. You can close more deals (buy and sell a stock) as much as possible.

Note: You can not simultaneously involved in multiple transactions (you must sell before buying shares again before the fall).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Day 2 (= 1 stock price), when buying, on day 3 (stock price = 5) when sold, this transaction can profit = 5-1 = 4.
  Then, on day 4 (stock price = 3) When buying on day 5 (stock price = 6), when sold, this transaction can profit = 6-3 = 3.
Example 2:

Enter: [1,2,3,4,5]
Output: 4
to explain: (stock price = 1) when buying on Day 1, on Day 5 (stock price = 5), when sold, this Exchange can profit = 5-1 = 4.
  Note that you can not buy stock in a series of 1 and day 2, then after they are sold.
  Because it belongs to the same time involved in multiple transactions, you have to sell the stock before the fall before buying again.
Example 3:

Enter: [7,6,4,3,1]
Output: 0
to explain: In this case, no transaction is completed, the maximum profit is zero.

 

Chicken dishes to try:

Thinking more clearly, to find each piece of the riser, the starting point of ascent that buy point, the focus ascent is selling point. So start writing code, then debug the code three minutes ten minutes. Although the idea is simple, but there are still some details to note:

  • Can only be sold in buying the premise can only be bought after selling
  • If the length parameter passed is 0 or 1, 0 is output directly profit
  • Note number ranging from bell can take, etc. (easily fall case)
  • If the first day is less than the next day, mandatory buy
  • If the last day or did not meet the conditions to sell, sell mandatory
. 1  class Solution {
 2  public :
 . 3      int MAXPROFIT (Vector < int > & Prices) {
 . 4          int Profit = 0 ;
 . 5          int inPrice;
 . 6          int size = prices.size ();
 . 7          int I;
 . 8          int In Flag = 0 ; / / flag is in the state to buy (sell state 1) 
. 9          iF (size == 0 || size == 1 ) return  0 ;
 10          iF (Prices [ 0] <= prices[1]) {
11             inPrice = prices[i];
12             flag = 1;
13         }
14         for (i = 1; i < size - 1; i ++) {
15             if (prices[i - 1] >= prices[i] && prices[i] <= prices[i + 1] && flag == 0) {
16                 inPrice = prices[i];
17                 flag = 1;
18             } else if (prices[i] <= prices[i + 1] && flag == 1) {
19                 continue;
20             } else if (flag == 1){
21                 profit = profit + prices[i] - inPrice;
22                 flag = 0;
23             }
24         }
25         if (flag == 1) {
26             profit = profit + prices[i] - inPrice;
27             flag = 0;
28         }
29         return profit;
30     }
31 };

Of course, this code is still not satisfied, that is not very elegant, trivial cases to consider a bit more, see the code so big brother

 

Worship Gangster Code:

Harm, my thoughts were buying and selling stocks crucified. Although the idea of ​​greedy big brother like me, but he was not crucified the sale of this operation, so the code is very simple and elegant.

Although interest is equal to the difference between selling and buying prices, remove scenes can also be understood as an incremental value (that is, the sum of each incremental, so it is often incremental cumulative time, you do not need no incremental cumulative), plain and simple

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int>& prices) {
 4         int profit = 0;
 5         int size = prices.size();
 6         for (int i = 0; i < size - 1; i++) {
 7             int diff = prices[i + 1] - prices[i];
 8             if (diff > 0) {
 9                 profit += diff;
10             }
11         }
12         return profit;
13     }
14 };

 

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/xyy999/p/11901755.html