Leetcode_121_Best Time to Buy and Sell Stock
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/43024967
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.
思路:
(1)题意为给定一个数组,数组中第i个元素的值对应着第i天的股票,求只进行一次交易,即买入一次并卖出,所能得到的最大利润。
(2)该题考查的是最大差值。对于本题,如果第i天的值大于第i+1天的值,呈现单调递减就不能买;只有第i天的值小于第i+1天的值,呈现单调递增时买才能获利。所以,遍历数组,分别比较其中相邻的元素之差,获得的最大值即为最大利润。
(3)希望本文对你有所帮助。
算法代码实现如下:
/** * @authod liqq */ public int maxProfit(int[] prices) { int len = prices.length; if (prices == null || len <= 1) return 0; int curr = prices[0]; int profit = 0; for (int i = 0; i < len; i++) { int nex = prices[i]; profit = nex - curr > profit ? nex - curr : profit; curr = curr < nex ? curr : nex; } return profit; }
Leetcode_121_Best Time to Buy and Sell Stock的更多相关文章
- [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 ...
随机推荐
- Zookeeper的安装配置及基本开发
一.简介 Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等等. ZooKeeper的目标就 ...
- 深度学习与计算机视觉系列(2)_图像分类与KNN
作者: 寒小阳 &&龙心尘 时间:2015年11月. 出处: http://blog.csdn.net/han_xiaoyang/article/details/49949535 ht ...
- python将nan, inf转为特定的数字
最近,处理两个矩阵的点除,得到结果后,再作其他的计算,发现有些内置的函数不work:查看得到的数据,发现有很多nan和inf,导致python的基本函数运行不了,这是因为在除的过程中分母出现0的缘故. ...
- SQLite Select 语句(http://www.w3cschool.cc/sqlite/sqlite-select.html)
SQLite Select 语句 SQLite 的 SELECT 语句用于从 SQLite 数据库表中获取数据,以结果表的形式返回数据.这些结果表也被称为结果集. 语法 SQLite 的 SELECT ...
- mvn管理项目jar包
Maven是一个采用纯Java编写的开 源项目管理工具.Maven采用了一种被称之为project object model (POM)概念来管理项目,所有的项目配置信息都被定义在一个叫做POM.xm ...
- iOS9中如何在日历App中创建一个任意时间之前开始的提醒(三)
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 四.创建任意时间之前开始的提醒 现在我们找到了指定源中的指定日 ...
- JDBC-数据库的编程(一)
因为我使用的mysql数据库客户端程序是workBench,所以会用workBench来进行讲解. create table tbl_user( id int(11) unsigned not nul ...
- 剑指Offer——银行网申内容模版
剑指Offer--银行网申内容模版 年获得"优秀共青团员"称号 2013.12 科技标兵 2013.10 三好学生 2013.10 "三下乡"优秀学生 2013 ...
- Shell 整数比较、字符串比较
整数比较 -eq 等于,如:if [ "$a" -eq "$b" ] -ne 不等于,如:if [ "$a" - ...
- Android的DatePicker和TimePicker-android学习之旅(三十八)
DatePicker和TimePicker简介 DatePicker和TimePicker是从FrameLayout继承而来,他们都是比较简单的组件.时间改变时间分别添加OnDateChangeLis ...