《leetCode》:Best Time to Buy and Sell Stock III
2016-02-28 11:44
316 查看
题目
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.
思路一:报超时错误
思路:分而治之,将prices分成两个部分,分别求这两部分的最大值,每个部分只允许存在一次交易 ;时间复杂度为O(n^2);实现代码如下:
int getMaxProfitOneTransactions(int* prices, int pricesSize){ if(prices==NULL||pricesSize<1){ return 0; } int max=0; int buyPrice=prices[0]; for(int i=0;i<pricesSize;i++){ if(prices[i]<buyPrice){ buyPrice=prices[i]; } else{ int profit=prices[i]-buyPrice; max=(max>profit)?max:profit; } } return max; } int maxProfit(int* prices, int pricesSize) { if(prices==NULL||pricesSize<1){ return 0; } int max=0; for(int i=0;i<pricesSize;i++){ int maxPartOne_Two=getMaxProfitOneTransactions(prices,i)+getMaxProfitOneTransactions(prices+i,pricesSize-i); if(maxPartOne_Two>max){ max=maxPartOne_Two; } } return max; }
思路二
思路还是上面的思路,只是就空间来换取时间/* 思路:分而治之,将prices分成两个部分,分别求这两部分的最大值 解释: 首先,因为能买2次(第一次的卖可以和第二次的买在同一时间),但第二次的买不能在第一次的卖左边。 所以维护2个表,f1和f2,size都和prices一样大。 意义: f1[i]表示 -- 截止到i下标为止,左边所做交易能够达到最大profit; f2[i]表示 -- 从i下标开始,右边所做交易能够达到最大profit; 那么,对于f1 + f2,寻求最大即可。 */ int max(int a,int b){ return (a>b)?a:b; } int maxProfit(int* prices, int pricesSize) { if(prices==NULL||pricesSize<1){ return 0; } int *f1=(int *)malloc(pricesSize*sizeof(int)); int *f2=(int *)malloc(pricesSize*sizeof(int)); if(f1==NULL||f2==NULL){ exit(EXIT_FAILURE); } //先看第一个交易 f1[0]=0; int buyPriceMin=prices[0]; for(int i=1;i<pricesSize;i++){ if(prices[i]<buyPriceMin){ buyPriceMin=prices[i]; f1[i]=f1[i-1];//注意 } else{ int profit=prices[i]-buyPriceMin; f1[i]=max(f1[i-1],profit); } } //再看第二个交易,从右到左,寻找交易利润最大 f2[pricesSize-1]=0; int SellPriceMax=prices[pricesSize-1]; for(int i=pricesSize-2;i>=0;i--){ if(SellPriceMax<prices[i]){ SellPriceMax=prices[i]; f2[i]=f2[i+1];//注意 } else{ int profit=SellPriceMax-prices[i]; f2[i]=max(f2[i+1],profit); } } //在f1+f2中寻找的最大值就是最大利润值 int maxProfit=0; for(int i=0;i<pricesSize;i++){ maxProfit=max(maxProfit,f1[i]+f2[i]); } return maxProfit; }
上面的精简版本
int max(int a,int b){ return (a>b)?a:b; } int min(int a,int b){ return (a<b)?a:b; } int maxProfit(int* prices, int pricesSize) { if(prices==NULL||pricesSize<1){ return 0; } int *f1=(int *)malloc(pricesSize*sizeof(int)); int *f2=(int *)malloc(pricesSize*sizeof(int)); if(f1==NULL||f2==NULL){ exit(EXIT_FAILURE); } //先看第一个交易 f1[0]=0; int buyPriceMin=prices[0]; for(int i=1;i<pricesSize;i++){ buyPriceMin=min(buyPriceMin,prices[i]); f1[i]=max(f1[i-1],prices[i]-buyPriceMin); //用上面两行替换到下面被注释的部分 // if(prices[i]<buyPriceMin){ // buyPriceMin=prices[i]; // } // else{ // int profit=prices[i]-buyPriceMin; // f1[i]=max(f1[i-1],profit); // } } //再看第二个交易,从右到左,寻找交易利润最大 f2[pricesSize-1]=0; int SellPriceMax=prices[pricesSize-1]; for(int i=pricesSize-2;i>=0;i--){ SellPriceMax=max(SellPriceMax,prices[i]); f2[i]=max(f2[i+1],SellPriceMax-prices[i]); //用上面两行替换到下面被注释的部分 // if(SellPriceMax<prices[i]){ // SellPriceMax=prices[i]; // } // else{ // int profit=SellPriceMax-prices[i]; // f2[i]=max(f2[i+1],profit); // } } //在f1+f2中寻找的最大值就是最大利润值 int maxProfit=0; for(int i=0;i<pricesSize;i++){ maxProfit=max(maxProfit,f1[i]+f2[i]); } return maxProfit; }
相关文章推荐
- Gson 设定固定的日期格式来生成Gson-------Gson 可以将某个对象转换成json,也可将json转换成对象
- python_Python中用format函数格式化字符串的用法
- tcp有限状态机
- java中array,arraylist,iterator;
- The 11th Zhejiang Provincial Collegiate Programming Contest->Problem G:G - Ternary Calculation
- 读写给大家看的项目管理书
- 英语总结系列(十四):换一种心情充满趣味的二月
- 关联式容器map/multimap之元素个数统计、查找元素及元素的随机访问
- 简单选择排序
- 在vim下,实现nesC语句的高亮
- The 11th Zhejiang Provincial Collegiate Programming Contest->Problem G:G - Ternary Calculation
- 机电一体化-------学习笔记一
- <转> JAVA CAS原理深度分析
- Win7系统Oracle11g服务端和客户端连接数据库配置
- poj3463 最短路和次短路 计数
- 个人学习-java-非static静态代码块
- Android APP架构的那点事儿[MVP架构搭建]
- Hibernate程序碰到的两个问题
- Js事件列表
- 封装json ajax函数