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 (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
  Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.
Example 2:

Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
  Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
  engaging multiple transactions at the same time. You must sell before buying again.
##### 分析:

和另外一道类似,但是不同的在于可以多次买卖,但是手中同时进行的交易不能大于1个。

###### 方法一:

用max保存最大值,min保存最小值,res保存临时收益,profit保存最终收益,当当前价格比max小时应该结束当前交易,从当前价格买入,然后更新min,max都是为当前价格,temp加入profit,temp归零;如果当前价格比max大时,继续当前交易,更新max。时间复杂度O(n),空间复杂度O(1)

public int maxProfit(int[] prices) {
if(prices==null ||prices.length==0)
return 0;
int min=prices[0], max=prices[0];
int profit=0;
int temp = max-min;
for(int i=1;i<prices.length;i++){
if(prices[i]<max){
temp = max-min;
profit += temp;
temp = 0;
min = prices[i];
max = prices[i];
}
else{
max = prices[i];
temp = max-min;
max = prices[i];
}
}
profit += temp;
return profit;
}
###### 方法二:Peak Valley Approach

TotalProfit=∑i​(height(peaki​)−height(valleyi​))

和方法一差不多,在一个while循环中首先找valley,遇到升高就开始找peak.再次降低结束这次循环。继续开始搜索下一个上坡。

Time complexity : O(n)O(n). Single pass.

Space complexity : O(1)O(1). Constant space required.

public int maxProfit(int[] prices) {
int i = 0;
int valley www.dasheng178.com= prices[0];
int peak = prices[0];
int maxprofit = 0;
while (i < prices.length - 1) {
while (i < prices.length - 1 && prices[www.xiaomiyulezc.com ] >= prices[i + 1])
i++;
valley = prices[i];
while (i < prices.length - 1 && prices[i] <= prices[i + 1])
i++;
peak = prices[i];
maxprofit += peak - valley;
}
return maxprofit;
}
方法三:

只关注上坡,然后将每一段的profit相加

Time complexity : O(n)O(n). Single pass.

Space complexity : O(1)O(1). Constant space required.

public int maxProfit(int[] prices) {
int maxprofit www.michenggw.com= 0;
for (int i = 1; i <www.mhylpt.com/ prices.length; i++) {
if (prices[i] > prices[i - 1])
maxprofit += prices[i] - prices[i - 1];
}
return maxprofit;

最新文章

  1. PHP WAMP 文件上传 及 简单的上传预览
  2. codevs 1531 山峰
  3. IOS行货自动打包
  4. redis+keeplived分布式缓存
  5. php单例模式深入讲解
  6. Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树
  7. hdu 1075 What Are You Talking About(字典树)
  8. bootstrap3 响应式布局栅格式布局
  9. linux oracle 10g tar.gz :xhost: unable to open display
  10. 2017-3-25 css样式表(一)
  11. web前端自动化测试/爬虫利器puppeteer介绍
  12. SQL Server 2008 安装(lpt亲测)
  13. flask文件上传
  14. 对于Linux平台下C语言开发中__sync_函数的认识(转)
  15. 【转】snmpwalk常用用法
  16. 期中HTML代码及技术博客
  17. Spring Boot中使用MongoDB数据库
  18. 排查Hive报错:org.apache.hadoop.hive.serde2.SerDeException: java.io.IOException: Start of Array expected
  19. ecshop操作数据库类
  20. grpc 安装以及墙的解决方法

热门文章

  1. Python-内置函数3
  2. php 用continue加数字实现foreach 嵌套循环中止
  3. ThinkPHP开启设置子域名笔记
  4. Java 输出对象为字符串 工具类
  5. Python全栈 Web(HTML基础语法)
  6. Linux文件系统简介和软链接和硬链接的区别
  7. 某即时通信工具与RMS结合
  8. solidity事件详解
  9. Struts2中Action各种转发类型
  10. Spring Security 快速了解