Stock(买卖股票)

来源:互联网 发布:血糖检测数据 编辑:程序博客网 时间:2024/04/29 19:30

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 algorithm to find the maximum profit.Example 1:Input: [7, 1, 5, 3, 6, 4]Output: 5max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)Example 2:Input: [7, 6, 4, 3, 1]Output: 0In this case, no transaction is done, i.e. max profit = 0.

记录当前最小的买进价格,随后进行卖出,并且和当前最大的获利相比较,如果比当前最大的还要大,则更新当前最大值,否则继续执行,最后返回最大值。

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

2. 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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

可以无限次买卖股票,但是必须先卖了才能买新的,思路是从第二天开始,如果今日价格比昨天高,那么则可以昨日买入,今日卖出,而到了明日又可以重复该过程直到利润最大值。

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

3. Best Time to Buy and Sell Stock III

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).

第一种方法比较简单,就是当成两个两个stcokI 来求解,,即求[0,i],[i+1,n]两个最大值之和。时间复杂度优化后为O(n)。

class Solution {    public int maxProfit(int[] prices) {        if(prices.length < 2){            return 0;        }        int[] startprices = new int[prices.length];        int[] endprices = new int[prices.length];        for(int i=0; i<prices.length; i++){            startprices[i] = 0;            endprices[i] = 0;        }        int minprices = prices[0];        for(int i=1; i<prices.length; i++){            startprices[i] = Math.max(startprices[i-1], prices[i]-minprices);            minprices = Math.min(minprices, prices[i]);        }                int maxprices = prices[prices.length-1];        for(int i=prices.length-2; i>=0; i--){            endprices[i] = Math.max(endprices[i+1], maxprices-prices[i]);            maxprices = Math.max(maxprices, prices[i]);        }                int max = 0;        for(int i=0; i<prices.length; i++){            max = Math.max(max, startprices[i]+endprices[i]);        }        return max;    }}

4. Best Time to Buy and Sell Stock IV

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 **k** transactions.**Note:**You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).**Credits:**Special thanks to [@Freezen](https://oj.leetcode.com/discuss/user/Freezen) for adding this problem and creating all test cases.

这题使用动态规划
我们定义local[i][j]为在到达第i天时最多可进行j次交易并且最后一次交易在最后一天卖出的最大利润,此为局部最优。然后我们定义global[i][j]为在到达第i天时最多可进行j次交易的最大利润,此为全局最优。它们的递推式为:

local[i][j] = max(global[i - 1][j - 1] + max(diff, 0), local[i - 1][j] + diff)

global[i][j] = max(local[i][j], global[i - 1][j])

其中局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,和前一天的局部最优加上差值中取较大值,而全局最优比较局部最优和前一天的全局最优。
如果k的值远大于prices的天数,比如k是好几百万,而prices的天数就为若干天的话,上面的DP解法就非常的没有效率,应该直接用[Best Time to Buy and Sell Stock II 的方法来求解。

class Solution {    public int maxProfit(int k, int[] prices) {        int n = prices.length;        if(n<2){            return 0;        }        if(k>=n){            return maxstock(prices);        }        int[][] g = new int[n][k+1];        int[][] l = new int[n][k+1];        for(int i=1; i<n;i++){            int diff = prices[i]-prices[i-1];            for(int j=1;j<k+1;j++){                l[i][j] = Math.max(g[i-1][j-1]+Math.max(diff, 0), l[i-1][j]+diff);                g[i][j] = Math.max(g[i-1][j], l[i][j]);            }        }        return g[n-1][k];            }        public int maxstock(int[] prices){         if(prices.length < 2){            return 0;        }        int sum = 0;        for(int i=1; i<prices.length; i++){            if(prices[i-1]<prices[i]){                sum += prices[i]-prices[i-1];            }        }        return sum;    }}
阅读全文
0 0
原创粉丝点击