Best Time to Buy and Sell Stock II
2015-07-27 17:42
302 查看
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock
before you buy again).
e.g:
[3,2,4,10,11,6,4,7,8,3]
sum 会累加
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock
before you buy again).
public class Solution { public int maxProfit(int[] prices) { int total = 0; for (int i=0; i< prices.length-1; i++) { if (prices[i+1]>prices[i]) total += prices[i+1]-prices[i]; } return total; } }
e.g:
[3,2,4,10,11,6,4,7,8,3]
sum 会累加
相关文章推荐
- zookeeper无法启动"Unable to load database on disk"
- registerClass和registerNib的区别
- 让 Vim 支持 nginx 语法(syntax) 格式化配置文件
- supervisor的配置
- 标准Http协议支持六种请求方法
- 最长公共上升子序列 csu1120 病毒
- Notification 的基本知识
- HDOJ 1012 u Calculate e(水题)
- 80端口被NT kernel & System占用
- PHP中的SESSION机制
- 第一篇博客
- MVC interview question
- 【linux高级程序设计】(第九章)进程间通信-管道 2
- 运维必须掌握的Linux面试题
- iOS基础
- hdoj1106排序(sort)
- OC学习对象的存储细节,isa指针
- ActiveReport开发入门-列表的交互性
- JAVA里的别名机制
- MySql数据库连接操作