【leetcode】Best Time to Buy and Sell Stock I&II&III

原创 2015年07月06日 23:06:59

Problem 1

这里写图片描述

Hint

DP

Code

int maxProfit(int* prices, int pricesSize) {
    if(!pricesSize) return 0;
    int cur = 0;
    int max = 0;
    for(int i=1;i<pricesSize;++i){
        cur += prices[i]-prices[i-1];
        if(cur<0) cur = 0;
        else if(cur>max) max = cur;
    }
    return max;
}

Problem 2

这里写图片描述

Hint

Greedy

Code

int maxProfit(int* prices, int pricesSize) {
    int sum = 0;
    for(int i=1;i<pricesSize;++i){
        sum += prices[i]-prices[i-1]>0?prices[i]-prices[i-1]:0;
    }
    return sum;
}

Problem 3

这里写图片描述

Hint

DP

Code

//打印数组
void printArray(int *array,int len,char *note){
    printf("%s\n",note);
    for(int i=0;i<len;++i)
        printf("%d ",array[i]);
    printf("\n");
}

//简化操作
int *simple(int *prices,int *pricesSize){
    int i=0;
    for(int i=1;i<*pricesSize;++i)
        prices[i-1] = prices[i]-prices[i-1];
    --(*pricesSize);
    //printArray(prices,*pricesSize,"After subtraction:");
    bool flag = prices[0]>=0?true:false;
    int count = 0;
    for(int i=1;i<*pricesSize;++i){
        if(flag){
            if(prices[i]>=0)
                prices[count]+=prices[i];
            else{
                flag = false;
                prices[++count]=prices[i];
            }
        }else{
            if(prices[i]<=0)
                prices[count]+=prices[i];
            else{
                flag = true;
                prices[++count]=prices[i];
            }
        }
    }
    *pricesSize = count+1;
    return prices;
}

//获取当前段最大利润
int maxProfitNow(int *prices,int pricesSize){
    int max = 0;
    int cur = 0;
    for(int i=0;i<pricesSize;++i){
        cur += prices[i];
        if(cur<0){
            cur = 0;
        }
        max = max>cur?max:cur;
    }

    return max;
}

//获取最大利润
int maxProfit(int* prices, int pricesSize) {
    if(pricesSize==1)   return 0;
    //简化
    int *simplestPrices = simple(prices,&pricesSize);
    //printArray(simplestPrices,pricesSize,"After simple:");
    //printf("\n");
    int max = 0;

    for(int i=0;i<pricesSize;++i){
        if(simplestPrices[i]<0){
            int tmp = maxProfitNow(simplestPrices,i) + \
                      maxProfitNow(simplestPrices+i+1,pricesSize-i-1);
            max = tmp>max?tmp:max;
            //printArray(prices,pricesSize,"Test");
            //printf("%d %d--\n",i,tmp);
        }
    }
    int tmp = maxProfitNow(simplestPrices,pricesSize);
    max = tmp>max?tmp:max;

    return max;
}

Summary

You may figure it out,that these problems are as the same with “Maximum subarray”.
So in the third solution,I simplify the prices’ array.And use the DP to calculate the maximum result from some maximum subarray.

版权声明:Pain is just in your mind.

相关文章推荐

leetcode_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV

121. Best Time to Buy and Sell Stock [题目] Say you have an array for which the ith element is the p...

【leetcode】Array——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 pri...

LeetCode121/122/123/188 Best Time to Buy and Sell Stock<股票> I/II/III/IIII----DP+Greedy**

一:LeetCode 121 Best Time to Buy and Sell Stock 题目: Say you have an array for which the ith element...

LeetCode - Best Time To Buy and Sell Stock Series (I, II, III, IV, with Cooldown)

LeetCode - Best Time to Buy and Sell Stock 系列 解题报告本文记录笔者在解LeetCode上的Best Time to Buy and Sell Stock系...

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

Best Time to Buy and Sell Stock I题意:用一个数组表示股票每天的价格,数组的第i个数表示股票在第i天的价格。 如果只允许进行一次交易,也就是说只允许买一支股票并卖掉,求...
  • ljiabin
  • ljiabin
  • 2015年04月06日 11:10
  • 20663

leetcode Best Time to Buy and Sell Stock I & II & III

Best Time to Buy and Sell Stock  class Solution { public: int maxProfit(vector &prices) { ...

LeetCode 121-123 Best Time to Buy and Sell Stock I II III

LeetCode 121-123 Best Time to Buy and Sell Stock I II III 选择买卖股票的最佳时间,使得利润最大,主要采用动态规划或者贪心算法解决这三道题。...
  • Jh_SYSU
  • Jh_SYSU
  • 2017年08月05日 11:49
  • 167

leetcode day6 -- String to Integer (atoi) && 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: Careful...

LeetCode:best-time-to-buy-and-sell-stock I and II and III

推荐参照:Leetcode题目难度等级及面试频率总结题目描述:best-time-to-buy-and-sell-stock I  Say you have an array for which th...

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

题目1:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price o...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【leetcode】Best Time to Buy and Sell Stock I&II&III
举报原因:
原因补充:

(最多只允许输入30个字)