Best Time to Buy and Sell Stock
2015-07-10 17:35
302 查看
题目:
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.
reference: http://lifexplorer.me/leetcode-best-time-to-buy-and-sell-stock-i-ii-iii/
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.
public class Solution { public int maxProfit(int[] prices) { int MaxProfit=0; int MinElement=Integer.MAX_VALUE; for(int i=0;i<prices.length;i++) { MinElement=Math.min(MinElement,prices[i]); MaxProfit=Math.max(MaxProfit,prices[i]-MinElement); } return MaxProfit; } }
reference: http://lifexplorer.me/leetcode-best-time-to-buy-and-sell-stock-i-ii-iii/
相关文章推荐
- 判断链表中是否有环存在
- PyPI中国镜像 2015
- NSUserDefaults的读写
- 对各种语言的偏见
- 创建Imgbutton 按钮
- Swift入坑--单例
- hiho一下 连通性二·边的双连通分量
- MYSQL区分大小写
- java.lang.UnsupportedOperationException异常
- Android开发—Navigation Drawer(侧边栏菜单实现)
- 2015.7.10(全部涨停!想逢高出货,但是担心周一创新高)
- 手动挡停车技巧;换挡注意
- Search Insert Position
- Cocos2dx 3.6 CCLable enableShadow error.
- 【VMware】VMware Workstation中虚拟机网络配置
- Linux~centos安装LAMP环境
- WWDC15 Session笔记 - iOS 9 多任务分屏要点
- Java中类的继承与接口
- JQuery EasyUI学习框架
- 打箱子&地形&鼠标点击移动总结