121. Best Time to Buy and Sell Stock
2016-06-15 05:45
295 查看
顺着数组走,保存:
1.到目前为止最大profit
2.到目前为止最小price
更新两个数据,结尾返回maxProfit
1.到目前为止最大profit
2.到目前为止最小price
更新两个数据,结尾返回maxProfit
public int maxProfit(int[] prices) { if(prices.length < 1) { return 0; } int minPrice = prices[0]; int maxProfit = 0; for(int i = 1; i < prices.length; i++) { minPrice = (prices[i] < minPrice)? prices[i]: minPrice; maxProfit = (maxProfit < prices[i] - minPrice)? prices[i] - minPrice: maxProfit; } return maxProfit; }
相关文章推荐
- 传智播客c/c++公开课学习笔记--Linux网络流媒体服务器的核心代码揭秘
- LeetCode "Line Reflection"
- 关于C51释放按键与后沿消抖
- 正则表达式中跨行匹配问题
- leetcode #95 in cpp
- Python NLP相关工具
- 用 Visual C++ 2015 编译 FreeGLUT
- 120. Triangle
- 一个最简单的博客园文章密码暴力破解器-python3实现
- LeetCode 355. Design Twitter(设计Twitter)
- 统计学 分布篇 - Hypergeometric Distribution(超几何分布)
- python实现概率分布
- Factor Combinations
- [leetcode] 188. Best Time to Buy and Sell Stock IV 解题报告
- Linux软件管理器(如何使用软件管理器来管理软件)
- leetcode #94 in cpp
- Two Sum
- macOS Sierra内置功能更新-AFPS
- OpenStack Mitaka Horizon 主题开发
- Codeforces Round #357 (Div. 2) B. Economy Game