Leetcode73: Best Time to Buy and Sell Stock II
2015-09-22 14:17
323 查看
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).
允许无限次无时间重叠的买卖,求最大收益,可以用贪婪法。
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).
允许无限次无时间重叠的买卖,求最大收益,可以用贪婪法。
class Solution { public: int maxProfit(vector<int>& prices) { int res = 0; if(prices.empty()) return res; for(int i = 1; i < prices.size(); i++) { if(prices[i] > prices[i-1]) res += prices[i]-prices[i-1]; } return res; } };
相关文章推荐
- xmlHttp.status的值
- hdu5464 Clarke and problem(DP)
- List和Vector的简单用法(MFC)
- 模拟登录新浪微博
- PHP:Linux/Ubuntu15.04搭建LAMP开发环境图解教程
- Android的selector,背景选择器
- PLSQL Developer连接64位Oracle
- 清除作业列表SQL
- 第二次作业——结对项目之需求分析与原型模型设计
- ASP.NET MVC 3 初认知
- 用命令查看端口占用情况 netstat -ano
- 使用Eclipse Memory Analyzer进行内存泄漏分析三部曲
- win7 64位纯净版图标变成了一样该怎么办?win7旗舰版图标变成一样的解决方法
- Windows不能在本地计算机启动MongoDB,错误代码 100
- 紫金桥软件与LG K120S的RS485通讯
- java基本类型数组初始化
- 常用的正则表达式_总结
- 1001 The Cats' Feeding Spots (2015年北京赛区网络赛A题)
- 第八节 命令执行顺序控制与管道
- Java中的单例模式的总结