Best Time to Buy and Sell Stock

来源:互联网 发布:html css javascript 编辑:程序博客网 时间:2024/06/05 00:40


本题有一共有四中问题

1.

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

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.

从前向后,同时记录最小交易价格,到第i天的最大利润

class Solution {public:    int maxProfit(vector<int>& prices) {                int pricelength=prices.size();        if(pricelength==0)        return 0;        //vector<int> profit(pricelength-1);        int minprice=prices[0];        int maxprofit=0;        for(int i=1; i<pricelength; i++)        {            if(prices[i]<minprice)            minprice=prices[i];            else            {                if((prices[i]-minprice)>maxprofit)                {                    maxprofit=(prices[i]-minprice);                }            }        }        return maxprofit;    }};

2.

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

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

解析:每天的价格看作一个曲线,每个上升段的增加值累加,即每个极小值到极大值的增量累加,实现时相邻两天只要增加就对ans进行累加

代码:

class Solution {public:    int maxProfit(vector<int>& prices) {        int n=prices.size();        int ret=0;        for(int i=1; i<n; i++)        {           ret+=max(prices[i]-prices[i-1],0);        }        return ret;    }};

3.

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

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

解析:本题是第四题的一种情况,结合第四题,参考  点击打开链接      找到动态转移方程 

       在这里定义global[i][j]为前i天交易j次的最大收益,local[i][j]表示前i天交易j次的最大收益,且第i天进行了第j次交易

     diff=price[i]-price[i-1],

      状态转移方程为local[i][j]=max(local[i-1][j]+diff,global[i-1][j-1]+max(0,diff));

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

对于本题j最大为2

代码:

class Solution {public:    int maxProfit(vector<int>& prices) {                       if (prices.empty()||prices.size()<2)        return 0;         int day=prices.size();        vector<vector<int>>global(prices.size()+1,vector<int>(4,0));        vector<vector<int>>local(prices.size()+1,vector<int>(4,0));        for (int i=1; i<=day; i++)        {            int diff=prices[i-1]-prices[i-2];            if (i==1) diff=0;                        for (int j=1; j<3; j++)            {                local[i][j]=max(local[i-1][j]+diff,global[i-1][j-1]+max(0,diff));                global[i][j]=max(local[i][j],global[i-1][j]);            }        }        return global[day][2];    }};

4.

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

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

解析:本题参考第2题,但有两个问题需要注意

      a. 由于3中时间复杂度为O(n*k),在本题中如果k>n会tl ,这是就是可以交易任意次,按题2求解

      b. 3中空间复杂度为O(n*k*2),在本题中可能会ml,所以本题中可以使用滚动数组

代码:

class Solution {public:    int maxProfit(int k, vector<int>& prices) {        if (prices.empty()||prices.size()<2)        return 0;         int day=prices.size();         if(k>day) return maxprofit(prices);                 vector<vector<int>>global(prices.size()+1,vector<int>(k+1,0));        vector<vector<int>>local(2,vector<int>(k+1,0));                     for (int i=1; i<=day; i++)        {            int diff=prices[i-1]-prices[i-2];            if (i==1) diff=0;                        for (int j=1; j<k+1; j++)            {                local[i%2][j]=max(local[(i+1)%2][j]+diff,global[i-1][j-1]+max(0,diff));                global[i][j]=max(local[i%2][j],global[i-1][j]);            }        }        return global[day][k];                          }        int maxprofit(vector<int>& prices)    {        int ans=0;        for (int i=1; i<prices.size(); i++)        {            ans+=max(0,prices[i]-prices[i-1]);        }        return ans;    }    };


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 眼镜度数配高了怎么办 孩子近视800度可怎么办 儿童眼睛近视怎么办才能恢复正常 小孩眼睛近视怎么办才能恢复正常 3岁宝宝近视怎么办啊 6个月婴儿近视怎么办 近视眼的人老了怎么办 一千多度的近视怎么办 近视镜片磨花了怎么办 眼镜镜片磨花了怎么办 戴眼镜鼻梁有印怎么办 狗狗发烧怎么办最有效 狗狗感冒怎么办最有效 狗狗发烧去医院怎么办 眼睛里长了虫子怎么办 吃了没熟的猪肉怎么办 没熟的鸡肉吃了怎么办 狗狗大便有绦虫怎么办 吃了有虫的猪肉怎么办 米猪肉吃了会怎么办 吃了鱼的寄生虫怎么办 鱼身体里有线虫怎么办 幼猫半夜一直叫怎么办 猫半夜4点叫不停怎么办 眼睛里长了黄斑怎么办 眼睛周围长小疙瘩怎么办 眼睛被手机砸了怎么办 眼睛被东西砸了怎么办 眼睛被打了一下怎么办 眼睛被树枝伤了怎么办 眼球被打了一下怎么办 眼睛黄有血丝是怎么办 上眼皮有异物感怎么办 20岁眼周围皱纹怎么办 才22岁眼睛皱纹怎么办 眼周围发干皱纹怎么办 眼周围淤血肿了怎么办 每到秋季就咳嗽怎么办 左肾泥沙样结石怎么办 双肾泥沙样结石怎么办 温州市民卡丢了怎么办