[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 ...
随机推荐
- 利用python yielding创建协程将异步编程同步化
转自:http://www.jackyshen.com/2015/05/21/async-operations-in-form-of-sync-programming-with-python-yiel ...
- weblogic92 :BEA-101020
weblogic92 启动时后台出现错误:<BEA-101020> 解决方案: 1.删除临时文件: user_projects/domains/base_domain/servers/Ad ...
- SQLite打开提示database disk image is malformed
SQLite打开提示database disk image is malformed 网上说产生这种错误的原因有很多种,磁盘空间不足,还有就是写入数据过程中突然掉电等. 这种情况,如果数据还可以导出, ...
- (转)卸载SQLServer2008 数据库
1 卸载Microsoft SQL Server 2008主程序 1.1,控制面板-程序中找到“Microsoft SQL Server 2008”,双击卸载 1.2,弹出管理界面中选择“”删除“ 1 ...
- SQL执行的原理以及一些常见的关键字
sql语句在面试里面问道的问题: sql的解析的顺序 1.where里面的条件是从右向左扫描解析 2.from里面的大表在前,小表在后,解析的顺序是从右向左解析. 3.left/right/inner ...
- 一个关于发邮件的类,可以模拟发送对smtp服务器或者是本地文件夹
namespace SportsStore.Domain.Concrete { public class EmailSettings { public string MailToAddress = & ...
- VC++中,如何定义callback函数和它的触发事件?
对于回调函数的编写始终是写特殊处理功能程序时用到的技巧之一.先介绍一下回调的使用基本方法与原理. 1.在这里设:回调函数为A()(这是最简单的情况,不带参数,但我们应用的实际情况常常很会复杂),使用回 ...
- maven的版本管理笔记
1. 版本管理 (1) 快照版本对应了项目的开发过程,往往对应了很长的时间:发布版本对应了项目的发布,因此仅仅代表某个时刻项目的状态. (2) 理想的发布版本应当对应项目某个时刻比较稳定的状态,包括源 ...
- Android开发-API指南-<activity-alias>
<activity-alias> 英文原文:http://developer.android.com/guide/topics/manifest/activity-alias-elemen ...
- 没有终结点在侦听可以接受消息的*这通常是由于不正确的地址或者 SOAP操作导致的
引发原因:项目启动时,前端调用 wcf地址,引用的地址访问无法在 IIS Express找到导致该错误 解决方法,找出前端的web.config 查看引用的项目是什么地址开头,如 localho ...