leetcode 121. Best Time to Buy and Sell Stock-股市交易
2016-04-10 21:41
225 查看
原题链接:121.Best Time to Buy and Sell Stock
【思路】
本题考查动态规划。复杂度为n(O),在遍历过程中记录最小值min。结果 result 只取决于 result 与 prices[i] - min 中的较大值:
cases passed. Runtime: 2
ms Your runtime beats 60.20% of javasubmissions.
【思路】
本题考查动态规划。复杂度为n(O),在遍历过程中记录最小值min。结果 result 只取决于 result 与 prices[i] - min 中的较大值:
public int maxProfit(int[] prices) { int result = 0, min = 0x7fff_ffff; for (int price : prices) if (price < min) min = price; else result = Math.max(result, price - min); return result; }198 / 198 test
cases passed. Runtime: 2
ms Your runtime beats 60.20% of javasubmissions.
相关文章推荐
- Static block(class initializer)
- CSS3之选择器2(伪类)
- ACM刷题之HDU————最少拦截系统(贪心)
- 新浪微博启动时"欢迎回来"动画
- 网页版四则运算
- 大话设计模式(1)简单工厂模式
- 机器学习课堂笔记(九)
- GCD
- java中静态代码块、静态方法、static关键字详解
- 数据的解析 Gson的使用详解
- java基础(三),字符串
- c++友元函数及运算符重载
- mysql/01基础
- AfxBeginThread开启一个线程,怎样去关闭这个线程?
- 分类雏形
- ssh框架三者整合
- hdu2848 Visible Trees (容斥原理)
- 【160319 24:00】四则运算 3(结对开发 1)
- YCM和Ultisnips按键冲突解决方案(只使用TAB键,无错误)
- JAVA多线程和并发基础面试问答