【LintCode】买卖股票的最佳时机
来源:互联网 发布:郭德纲合作的网络平台 编辑:程序博客网 时间:2024/04/28 03:16
假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格。如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润。
样例
思路:简单贪心问题,注意处理边界问题给出一个数组样例 [3,2,3,1,2], 返回 1
代码如下:
public class Solution { /** * @param prices: Given an integer array * @return: Maximum profit */ public int maxProfit(int[] prices) { // write your code here if(prices.length <= 1) { return 0; } int ilow = prices[0]; int ians = 0; for(int i = 1;i < prices.length;i++) { if(prices[i] - ilow > ians) { ians = prices[i] - ilow; } if(prices[i] < ilow) { ilow = prices[i]; } } return ians; }}
阅读全文
0 0
- LintCode-买卖股票的最佳时机
- LintCode--买卖股票的最佳时机
- Lintcode:买卖股票的最佳时机
- 买卖股票的最佳时机,lintcode
- 【LintCode】买卖股票的最佳时机
- lintcode--买卖股票的最佳时机
- Lintcode 买卖股票的最佳时机
- 买卖股票的最佳时机-LintCode
- LintCode--买卖股票的最佳时机 II
- lintcode-买卖股票的最佳时机-149
- lintcode-买卖股票的最佳时机II-150
- LintCode:买卖股票的最佳时机 II
- LintCode:买卖股票的最佳时机 III
- Lintcode 买卖股票的最佳时机 II
- LintCode 151 买卖股票的最佳时机 III
- LintCode 152 买卖股票的最佳时机 IV
- lintcode--买卖股票的最佳时机II
- 买卖股票的最佳时机 II-LintCode
- IDEA激活方法
- nginx知识小结
- HTML5+CSS3从入门到精通书籍配套源码
- OSPF 5类 LSA 记忆
- P1725 琪露诺
- 【LintCode】买卖股票的最佳时机
- file and directory
- 基于大数据分析平台现状 规划和划分微服务粒
- R语言估算不同分类器的预测误差
- 使用浏览器的开发者工具来检查 CSS 的动画性能
- Gson解析(List和Map)格式json数据
- oschina支持svn
- HDU2255 奔小康赚大钱
- 斗地主