• Best Time to Buy and Sell Stock II


    Say you have an array for which the ith element is the price of a given stock on day i.

    Design an algorithm to find the maximum profit. You may complete at most two transactions.

    Note:
    You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

    Code:

    class Solution {
    public:
        int maxProfit(vector<int> &prices) {
            int cur=0;
            int maxProf=0;
            int tmpProf=0;
            for(int i=1;i<prices.size();i++){
                if(prices[i]<prices[i-1]){
                    cur=i;
                    maxProf+=tmpProf;
                    tmpProf=0;
                }else
                    tmpProf=prices[i]-prices[cur];
            }
            maxProf+=tmpProf;
            return maxProf;
        }
    };
  • 相关阅读:
    《java技术》第三次作业
    《Java技术》第二次作业
    《java技术》第一次作业
    java3
    java2
    java1
    开闭原则
    迪米特原则
    接口隔离原则
    里氏替换原则
  • 原文地址:https://www.cnblogs.com/winscoder/p/3398374.html
Copyright © 2020-2023  润新知