Best Time to Buy and Sell Stock I,II,III [leetcode]

Best Time to Buy and Sell Stock I

只能作一次操作时:维护preMin记录之前出现的最小值

代码如下:

    int maxProfit(vector<int> &prices) {
        if (prices.size() == 0) return 0;
        int profit = 0;
        int preMin = prices[0];
        for (int i = 1; i < prices.size(); i++)
        {
            if (prices[i] < preMin) preMin = prices[i];
            profit = max(profit, prices[i] - preMin);
        }
        return profit;
    }

Best Time to Buy and Sell Stock II

能无限次操作时:当==>当前价格 > 买入价格的时候,卖出

代码如下:

    int maxProfit(vector<int> &prices) {
        if (prices.size() == 0) return 0;
        int profit = 0;
        int buy = prices[0];
        for (int i = 1; i < prices.size(); i++)
        {
            if (buy < prices[i])
                profit += prices[i] - buy;
            buy = prices[i];
        }
        return profit;
    }

Best Time to Buy and Sell Stock III

只能操作两次时:

两次操作不能重叠,可以分成两部分:0...i的最大利润fst  和i...n-1的最大利润snd

代码如下:

    int maxProfit(vector<int> &prices) {
        if (prices.size() == 0) return 0;
        int size = prices.size();
        vector<int> fst(size);
        vector<int> snd(size);

        int preMin = prices[0];
        for (int i = 1; i < size; i++)
        {
            if (preMin > prices[i]) preMin = prices[i];
            fst[i] = max(fst[i - 1], prices[i] - preMin);
        }
        int profit = fst[size - 1];
        int postMax = prices[size - 1];
        for (int i = size - 2; i >= 0; i--)
        {
            if (postMax < prices[i]) postMax = prices[i];
            snd[i] = max(snd[i + 1], postMax - prices[i]);
            //update profit
            profit = max(profit, snd[i] + fst[i]);
        }
        return profit;
    }
时间: 2024-10-11 01:42:00

Best Time to Buy and Sell Stock I,II,III [leetcode]的相关文章

Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III

题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algori

[Lintcode] Best Time to Buy and Sell Stock I, II, III &amp;&amp; Maximum Subarray

买卖股票系列 && Subarray 相关题目: Best Time to Buy and Sell Stock I && II && III (IV 单独开贴) Maximum Subarray I && II 为什么这些我要总结到一起呢?因为思路基本一致. 题目简略: 买卖股票1: 一次买卖,利润最大. 买卖股票2: N次买卖,利润最大. 买卖股票3: 2次不重叠的买卖,利润最大. Maximum Subarray: Given an a

【LeetCode】 Best Time to Buy and Sell Stock I II III IV 解题报告

Best Time to Buy and Sell Stock I 题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格. 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求最大的收益. 分析:动态规划法.从前向后遍历数组,记录当前出现过的最低价格,作为买入价格,并计算以当天价格出售的收益,作为可能的最大收益,整个遍历过程中,出现过的最大收益就是所求. 代码:时间O(n),空间O(1). Best Time to Buy and Sell Stock II 题目:用一

[Leetcode][JAVA] Best Time to Buy and Sell Stock I, II, III

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

lintcode medium Best Time to Buy and Sell Stock I,II,III

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

leetcode--Best Time to Buy and Sell Stock i ii iii

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

LeetCode之“动态规划”:Best Time to Buy and Sell Stock I &amp;&amp; II &amp;&amp; III &amp;&amp; IV

1. Best Time to Buy and Sell Stock I 题目链接 题目要求: Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi

leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is inte

[Leetcode] Best Time to Buy and Sell Stock I,II,III,IV

三种股票交易算法 一.交易次数没有限制 使用贪心策略,找最长递增序列,同时累加相应利润. 二.只有一次交易 使用动态规划算法,从前往后,依次记记录相应时间节点前面的最小price,同时获得在这个节点的最大利润,同时更新最小price 三.最多两次 使用两次动态规划 1.从左向右,记录在相应的时间节点卖出的最大利润,实际上就是问题二 2.从右向左,记录在相应的时间节点买入的最大利润,因此这里需要记录最大的price. 将each 的left[index]+ right[index]曲最大值. 四.