[leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案。过了两道,第三道看不懂,放置,稍后继续。
一、Best Time to Buy and Sell Stock I
题目:一个数组表示一支股票的价格变换。要求只买卖一次,获得最大收益。
思路:一开始我认为是寻找最大、最小值,但由于最大值不一定总是出现在最小值的后面,因此WA。
参考思路:DP。对第i个价格,减去前i-1个价格中的最小值(保证该收益是在第i个价格卖出的最大收益),其收益与之前获得的最大收益相比。
代码:
public int maxProfit(int[] prices) {
int len = prices.length ;
if(len < 2) return 0;
int min = prices[0] ;
int maxProfit = 0;
for(int i = 1 ; i < len ; i++){
int temp = prices[i] - min; //当前值减去前i-1个值的最小值
if(maxProfit < temp) maxProfit = temp; //更新最大收益
if(prices[i] < min) min = prices[i]; //看是否需要更新前i个值的min值,用于下次循环
}
return maxProfit;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
二、Best Time to Buy and Sell Stock II
题目:在上一题的基础上,允许对一支股票任意次买卖,(同一时间可先卖出再马上买入),同样求最大收益。
思路:如果第i个价格大于第i-1个价格,则将此部分收益加入到最大收益中,因为可以在第i个价格处马上卖出再马上买入。
代码:
public int maxProfit(int[] prices) {
int profit = 0;
for(int i = 1 ; i < prices.length ; i++){
if(prices[i] > prices[i - 1]) profit += prices[i] - prices[i - 1];
}
return profit;
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
三、待续
[leetcode]_Best Time to Buy and Sell Stock I && II的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- Leetcode: Best Time to Buy and Sell Stock I, II
思路: 1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
随机推荐
- 为什么老师不喜欢RelativeLayout
这个要看个人喜好.RelativeLayout对于一些简单布局来说反而复杂了.要计算相对距离这些.用线性布局只要利用好weight可以很直观的实现效果.所以这个因个人喜好而定 对于讲一些简单的功能来说 ...
- SQL游标 更新
--定义游标 DECLARE cur_getaddress CURSOR FOR SELECT new_a ,new_b ,new_c ,new_d FROM table WHERE (new_a i ...
- js中格式化时间字符串
.net 程序员肯定有遇到过,将一个对象json序列化之后Date 字段 就会转化成 '/Date(1370770323740)/' 这种格式的数据,下面介绍一种在js中,关于时间格式的转换. < ...
- [SQL]把同一字段里的多行数据用一行显示
declare @t table(id int,num int) insert @t , union all , union all , --select * from @t ----查询 decla ...
- poj 2253 Frogger dijkstra算法实现
点击打开链接 Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21653 Accepted: 7042 D ...
- 【转】android的一些开源项目
自己一直很喜欢Android开发,就如博客副标题一样,我想做个好的App. 在摸索过程中,GitHub上搜集了很多很棒的Android第三方库,推荐给在苦苦寻找的开发者,而且我会不定期的更新这篇文章. ...
- MySQL主存复制与读写分离的感悟
1.主存复制: 就是实现数据拷贝,有点实时的感觉,完成数据同步,存储两份数据. 项目开发中,类似场景许多,尤其是异构系统之间的交互,协作.-------------------场景目的:为了安全,各自 ...
- js和jQuery前台校验文件大小
1.支持Google 不支持IE <script type="text/javascript" src="${pageContext.request.context ...
- VC++2005下的ADO SQL语句(like,count,distinct)和操作(转)
http://blog.sina.com.cn/s/blog_56fd66a70100hxjf.html http://timke.blog.163.com/blog/#m=0 环境:MFC Dia ...
- mysql命令整理0919 不定期更新中
1)新建数据库 create database +database_name: 查询数据库 show databases; 切换数据库 use database_na ...