[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 ...
随机推荐
- SQL 去特殊字符
)) ) as begin declare @i int while patindex('%[^%@+*,=../_ <>''" ^0-9 ^a-Z ^''- ^吖-座]%' , ...
- SQL Server中GO的使用方法(转)
GO不是标准SQL语句,甚至不是T-SQL语句.它只是SQL Server管理器(SSMS)中用来提交T-SQL语句的一个标志.你可以在SSMS中任意指定这个提交标志.SSMS->工具-> ...
- 数据库内置视图以及常见的DBMS开发包
如果想了解oracle运行的一些数据信息,oracle有一些视图可以供我们查询,通过这些内置的视图我们可以了解数据库 运行的一些信息,比如数据库文件存在什么地方.有哪些表空间.表空间的利用率.orac ...
- SparkSQL External Datasource简易使用之CSV
下载源码&编译: git clone https://github.com/databricks/spark-csv.git sbt/sbt assembly Maven GAV: group ...
- Hadoop JobHistory
hadoop jobhistory记录下已运行完的MapReduce作业信息并存放在指定的HDFS目录下,默认情况下是没有启动的,需要配置完后手工启动服务. mapred-site.xml添加如下配置 ...
- JavaScript对象的创建之使用json格式定义
json: javascript simple object notation. json就是js的对象,但是它省去了xml中的标签,而是通过{}来完成对象的说明. 定义对象 var person = ...
- 制作Win7(x86)PE ISO文件
WinPE3.1 —Win7 x86 PE V3.1: waik_supplement_zh-cn.isoDVD: cn_windows_7_professional_with_sp1_x86 ...
- 零基础如何入门Python
编程零基础如何学习Python 如果你是零基础,注意是零基础,想入门编程的话,我推荐你学Python.虽然国内基本上是以C语言作为入门教学,但在麻省理工等国外大学都是以Python作为编程入门教学的. ...
- 实时阴影渲染(一):PSSM平行分割阴影图
PSSM(Parallel Split Shadow Map)平行分割阴影图,是一种根据距离远近采用多个深度纹理渲染阴影的方法 适合用于室外大场景中的平行光比如太阳形成的阴影 本系列需要读者了解基本的 ...
- (转载)MonoBehaviour的事件和具体功能总结
分享一点MonoBehaviour的事件和具体功能总结的基础知识,苦于Visual Studio 2013没有对MonoBehaviour的行为做出智能提示,写个函数都要全手打,记性好的将就着手打,脑 ...