Best Time to Buy and Sell Stock i
Say you have an array for which the ith element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 max. difference = 6-1 = 5 (not 7-1 = 6, as selling price needs to be larger than buying price)
Example 2:
Input: [7, 6, 4, 3, 1]
Output: 0 In this case, no transaction is done, i.e. max profit = 0
只允许一次交易,算出最大利益。利益是卖的价格减去买的价格,卖在买后面。
这个题目的变形出现过:一个数组,0<=a<b<=n。。。a,b是数组的两个下标,b在后面。求num[b]-num[a]的最大值。跟这个题是一个意思。
依次遍历数组,每个元素跟它前面的最小值做差,会使得此元素的差值最大,然后找出最大差值就行。见代码。
class Solution {
public int maxProfit(int[] prices) {
if(prices==null||prices.length<2) return 0;
int maxProfit=0;
int min=prices[0];
for(int i=1;i<prices.length;i++){
if(prices[i]-min>maxProfit)
maxProfit=prices[i]-min;
if(min>prices[i])//找出当前元素前面的最小值,在一次遍历的时候就可以找出来了
min=prices[i];
}
return maxProfit;
}
}
Best Time to Buy and Sell Stock i的更多相关文章
- [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 ...
- [LintCode] 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 ...
- [LintCode] 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 (股票买卖时机问题2)
问题: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- 123. Best Time to Buy and Sell Stock (三) leetcode解题笔记
123. Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the pric ...
- 122. Best Time to Buy and Sell Stock(二) leetcode解题笔记
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
随机推荐
- Android初级教程获取手机系统联系人信息
在手机内部,对联系人信息存在对应的数据库.我们创建的而联系人信息都存在这张表中.如下是对数据库的截图,我已经对表和应该注意的地方做了红笔标注: 好了,现在可以根据数据库里面的数据来写代码了. 代码如下 ...
- 概率论:假设检验-t检验和Augmented Dickey–Fuller test
http://blog.csdn.net/pipisorry/article/details/51184556 T检验 T检验,亦称student t检验(Student's t test),学生t检 ...
- RabbitMQ消息队列的小伙伴: ProtoBuf(Google Protocol Buffer)
什么是ProtoBuf? 一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化.它很适合做数据存储或 RPC 数据交换格式.可用于通讯协议.数据存储等领域的语言无关.平台无关.可扩 ...
- Spark SQL官方文档阅读--待完善
1,DataFrame是一个将数据格式化为列形式的分布式容器,类似于一个关系型数据库表. 编程入口:SQLContext 2,SQLContext由SparkContext对象创建 也可创建一个功能更 ...
- 【一天一道LeetCode】#82. Remove Duplicates from Sorted List II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- Mybatis插件原理分析(三)分页插件
在Mybatis中插件最经常使用的是作为分页插件,接下来我们通过实现Interceptor来完成一个分页插件. 虽然Mybatis也提供了分页操作,通过在sqlSession的接口函数中设置RowBo ...
- (四十九)Quartz2D自定义控件
利用Quartz2D来自定义UIImageView: 模仿UIImageView: 设置frame,设置图片. 注意一个细节,自定义的imageView,应该通过重写set方法来设置图片并且重绘,否则 ...
- python一行写不下,变多行
python里一行写不下,拆成多行, \和() 两种方法 在一行末尾 加上" \",也就是空格加上\ a= 'sdfaf' \ 'test' 注意两个对象都要独立,字符串 ...
- 第一篇、vlc-android之开篇介绍
转载请注明出处:http://blog.csdn.net/cuiran/article/details/30054835 最近一直研究android的视频直播部分,从最开始的直接播放本地视频文件,到使 ...
- python3爬虫 - 利用浏览器cookie登录
http://blog.csdn.net/pipisorry/article/details/47980653 爬虫爬网站不免遇到需要登录的问题. 登录的时候可能还会碰到需要填验证码的问题, 有的验证 ...