leetcode122 Best Time to Buy and Sell Stock II
来源:互联网 发布:mac 重装 u盘 编辑:程序博客网 时间:2024/05/27 20:51
我的leetcode代码在github地址:https://github.com/gaohongbin/leetcode
题目:
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).
Subscribe to see which companies asked this question
这个题和leetcode121 Best Time to Buy and Sell Stock差不多,只是leetcode121 Best Time to Buy and Sell Stock只能完成一次交易,而leetcode122 Best Time to Buy and Sell Stock II可以完成多次交易。求出最大的收益。
思路:
这个题刚开始没有思路,在纸上画一画我们可能有点思路了,我自己的思路是这样的。
如果数组是2,4,5,8这种递增的数组,则8-2就是能得到的最大利润。
如果数组为2,5,4,8这种数组,则(5-2)+(8-4)就是得到的最大利润,如果一个数组在递增的过程突然不再递增,则就可以进行一次交易了。
如果数组为4,3,2,5这种数组,则在递减到最低点时买入,在相邻的一个最高点卖出。
根据这个思路,我写下了如下的代码:
代码:
public int maxProfit(int[] prices) { int length=prices.length; if(prices==null || length<2) return 0; int maxProfit=0; int price=prices[0]; int lastSellPrice=prices[0]; for(int i=1;i<length;i++){ if(prices[i]>price){ if(prices[i]>lastSellPrice) lastSellPrice=prices[i]; if(prices[i]<lastSellPrice){ maxProfit+=lastSellPrice-price; price=prices[i]; lastSellPrice=price; } } if(prices[i]<=price){ if(lastSellPrice>price){ maxProfit+=lastSellPrice-price; price=prices[i]; lastSellPrice=price; } if(lastSellPrice==price){ price=prices[i]; lastSellPrice=price; } } } maxProfit+=lastSellPrice-price; return maxProfit; }
但是最后运行以后才击败了6.2%的代码,发现还有另外一个代码,感觉很精练,最后才发现自己的代码最终实现的也是这样的功能。
public int maxProfit(int[] prices) { if (prices.length < 2) return 0; int maxProfit = 0; for (int i = 1; i < prices.length; i++) { int diff = prices[i] - prices[i - 1]; if (diff > 0) { maxProfit += diff; } } return maxProfit; }
- leetcode122---Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- leetcode122 Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- leetcode122 Best Time to Buy and Sell Stock II
- Leetcode122. Best Time to Buy and Sell Stock II
- leetcode122.[DP] Best Time to Buy and Sell Stock II
- LeetCode122—Best Time to Buy and Sell Stock II
- leetcode122. Best Time to Buy and Sell Stock II
- leetcode122~Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II题解
- LeetCode122 Best Time to Buy and Sell Stock II
- Leetcode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122——Best Time to Buy and Sell Stock II
- Leetcode122 - Best Time to Buy and Sell Stock II (dp)
- 文章标题
- 七牛CEO许式伟:服务端开发那些事儿
- latex算法步骤如何去掉序号
- java 异常 集合
- UVa11159 - Factors and Multiples(二分匹配匈牙利算法)
- leetcode122 Best Time to Buy and Sell Stock II
- CSS3 Animation 帧动画
- POJ 2449 Remmarguts' Date(第k短路+spfa+A*搜索)
- python 对文件做类别标签
- HDOJ 2010 水仙花数
- hdu 1013
- yum 快速搭建lnmp环境
- 水生火热的互联网金融
- Java 图像 处理