Best Time to Buy and Sell Stock系列问题题解

来源:互联网 发布:用友软件工资模块 编辑:程序博客网 时间:2024/06/10 10:52

本来只是想做一下第四个题,奈何里面的细节有些地方不懂,只能从第一题开始做,心累啊~

-------------------------第一题题目------------------------

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.

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.
----------------------第一题题解------------------------------------

这道题的难度是easy,所以也没什么好说的,题目大意就是找一个时间点买入,再找一个时间点卖出,差价要最大,当然了时间是有先后顺序的。我想的是直接找到所有的差价,然后得到最大的那一个,时间复杂度N^2,有大神用了N的复杂度就做出来了,思路大概是从前到后遍历,每次都找到当前最小的那个数,然后用当前的数去减这个最小的数与已经保存的最大的差价比较,保留大的那个。最后得到的就是全局差价最大了。下面是我的代码,也就是复杂度为N^2的那个,时间复杂度为N的那个方法大家就自己写吧。

class Solution {public:    int maxProfit(vector<int>& prices) {        int l=prices.size();        int ans=0;        for(int i=0;i<l;i++)        for(int j=i;j<l;j++)        {        if(prices[j]-prices[i]>0)        ans=max(ans,prices[j]-prices[i]);}return ans;}};
-------------------------第二题题目---------------------------------

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

--------------------------第二题题解----------------------------------

第二题在第一题的基础上,不限交易次数,也就是你想买多少次就可以买多少次,但是同一天只能买入或者卖出。本来想想这道题不是那么容易的,然而难度却是easy,所以此题必有蹊跷,大家可以想象这个一个柱状图,如果前三个都是递增的柱子,比如1-2-3那么我们最大收益是多少呢?答案是2,怎么算的?用3减去1,没错。那么有趣的地方来了,我们同样可以使用2-1+3-2,在这个柱状图中,我们需要得到的是最大的差,而这个最大的差又是可以通过两个小差价来合在一起得到。这么一来的话我们知道得到相邻两个数所有大于0的差的和就行了。

class Solution {public:    int maxProfit(vector<int>& prices) {        int l=prices.size();        int ans=0;for(int i=1;i<l;i++){ans+=max(prices[i]-prices[i-1],0);}return ans;}};
-----------------------第三题题目-------------------------------

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) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

prices = [1, 2, 3, 0, 2]maxProfit = 3transactions = [buy, sell, cooldown, buy, sell]
-----------------------第三题题解-------------------------------

这道题在第二题的基础上增加了休市,即在卖出后的一天内不允许交易,那么就不能像第二题那样玩小聪明了,真正要上动态规划了。我们用dp【i】来表示第i天得到的最大收益,因为不限交易次数,所以要尽可能多的将差价加到一起,所以我们只假设只休息一天,那么如果说j+2天的价格低于第i天,我们就把这个差价加上去,否则dp【i】=max(dp【i】,dp【j】)。

class Solution {public:    static int maxProfit(vector<int>& prices) {    int l=prices.size();    if(l<2)    return 0;    int dp[l];    for(int i=0;i<l;i++)    {    dp[i]=0;    for(int j=0;j<i;j++)    {    dp[i]=max(prices[i]-prices[j],dp[i]);    if(i>j+2)    dp[i]=max(dp[i],dp[j]+max(prices[i]-prices[j+2],0));    else    dp[i]=max(dp[i],dp[j]);}}return dp[l-1];}};
--------------------------第四题题目--------------------------

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

-----------------------第四题题解------------------------

这一次只能允许最多交易两次,我的想法是找到一个分割点,得到这个分割点前后的最大利润,最后就可以得到全局的最大收益了,时间复杂度N^2,

class Solution {public:    int maxProfit(vector<int>& prices) {    int l=prices.size() ;    if(l<2)    return 0;    int ans=0;int pre[l];pre[0]=0;int post[l];post[l-1]=0;int mp=prices[0];for(int i=1;i<l;i++){mp=min(mp,prices[i]);pre[i]=max(pre[i-1],prices[i]-mp); }  int map=-prices[l-1]; for(int i=l-2;i>=0;i--) { map=min(map,-prices[i]); post[i]=max(post[i+1],-prices[i]-map); } for(int i=0;i<l;i++) { ans=max(ans,pre[i]+post[i]); }return ans;    }};
有大神仅仅使用了四行代码就搞定了,而且复杂度只有N,然而我却没看懂,感兴趣的小伙伴可以自己上LeetCode上去看一看,虽然看不懂,但我的内心觉得真的很厉害。

-----------------------------第五题题目--------------------------

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

------------------------第五题题解------------------------

好了,这次就是最多交易k次了,原谅我没有A掉这道题,在第208个测试点超时了,然后想了想优化方法,没有成功,所以就不放  题解了,等我有朝一日实力大增,再来A掉这道题吧。

------------------------手动分割线----------------------------

see you next illusion




原创粉丝点击