LeetCode | Best Time to Buy and Sell Stock

100 篇文章 0 订阅
4 篇文章 0 订阅

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), design an algorithm to find the maximum profit.

Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5

max. 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: 0

In this case, no transaction is done, i.e. max profit = 0.

反应了一会才看懂题目的意思;
原来就是差不多类似股票,要求最低的时候买进然后最高的时候卖出,让我们找出最大的差价。
我们可以从头向后遍历,记录最小值最大值和最小最大值的位置。
最大值要求不低于最小值点的大小,最重要一点是:
最大值必须出现在最小值的后面
使用一个中间变量记录maxV-minV的大小

class Solution {
public:
    int maxProfit(vector<int>& prices) {
        int n=prices.size();
        int minV=INT_MAX,minPos=0,maxV=INT_MIN,maxPos=0;
        int sum=INT_MIN;
        for(int i=0;i<n;i++){
            if(prices[i]<minV){
                minV=min(minV,prices[i]);
                minPos=i;
                //如果小数出现在大数后面
                if(maxPos<minPos) maxV=minV;
            }
            if(prices[i]>maxV){
                maxV=max(maxV,prices[i]);
                maxPos=i;
            }
            //最大的结果可能出现在中间过程中
            sum=max(maxV>minV?maxV-minV:0,sum);
        }
        return sum>0?sum:0;
        // vector<int> dp(n,1);
        // for(int i=0;i<n;i++){
        //     for(int j=0;j<i;j++){
        //         if(dp[i]<dp[j]+1 && prices[j]<prices[i])
        //         dp[i]=dp[j]+1;
        //     }
        // }
        // printf("dp[n] %d\n",dp[n-1]);
    }
};

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

II具有一个很奇特的贪心性质,也即:
例如对于1,2,5,7
7-1=(2-1)+(5-2)+(7-5) 也就是说对于股价连续提升的区间,第一天买最后一天卖,和每天买卖,所得利润是一样的。

于是对于nums[i]>nums[i-1],即可立即卖出和买入。
检测到nums[i]

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

        // for(int i=0;i<n;i++){
        //     //判断股票是下降趋势
        //     if(i<n-1 && prices[i+1]-prices[i]<0){
        //         sum+=prices[i]-prices[start];
        //         start=i+1;
        //     }

        //     //最后一天,抛售股票
        //     if(i==n-1) sum+= (prices[i]-prices[start])>0?(prices[i]-prices[start]):0;
        // }
        return sum;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值