zoukankan      html  css  js  c++  java
  • best-time-to-buy-and-sell-stock-iii leetcode C++

    Say you have an array for which the i th 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).

    C++

    class Solution {
    public:
        int maxProfit(vector<int>& prices){
            int buy1 = INT_MIN;
            int sell1 = 0;
            int buy2 = INT_MIN;
            int sell2 = 0;
            for(int i = 0; i< prices.size(); i++){
                buy1 = max(buy1,-prices[i]);
                sell1 = max(sell1,buy1 + prices[i]);
                buy2 = max(buy2, sell1 - prices[i]);
                sell2 = max(sell2,buy2 + prices[i]);
            }
           return sell2;
        }
    };
  • 相关阅读:
    01模拟面试面试题汇总
    第一轮面试
    大觅网03Day
    大觅网02Day
    大觅网01Day
    树状数组
    HH的项链
    小z的袜子
    分块
    扩展欧几里德
  • 原文地址:https://www.cnblogs.com/vercont/p/10210293.html
Copyright © 2011-2022 走看看