【Leetcode】【Medium】Best Time to Buy and Sell Stock II
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).
解题:
将数组中每一个值都想象成数轴上的一个点,只要当前点比前一个点上升了,则此段收益就能够通过买入卖出获得;(即使是连续上升,可以中间段不卖出,收益也全都获得,因此不影响计算策略)。
代码:
class Solution {
public:
int maxProfit(vector<int> &prices) {
int size = prices.size();
int maxP = ;
for (int i = ; i < size; ++i) {
if (prices[i] > prices[i-])
maxP += prices[i] - prices[i-];
}
return maxP;
}
};
【Leetcode】【Medium】Best Time to Buy and Sell Stock II的更多相关文章
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- Algorithm - 贪心算法使用场景 ( LEETCODE —— Best Time to Buy and Sell Stock II)
先看一道leetcode题: Best Time to Buy and Sell Stock II Say you have an array for which the ith element is ...
- Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II)
Leetcode之动态规划(DP)专题-122. 买卖股票的最佳时机 II(Best Time to Buy and Sell Stock II) 股票问题: 121. 买卖股票的最佳时机 122. ...
- 【一天一道LeetCode】#122. Best Time to Buy and Sell Stock II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Say you ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- 【LeetCode OJ】Best Time to Buy and Sell Stock II
Problem Link: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ We solve this prob ...
- 【leetcode刷题笔记】Best Time to Buy and Sell Stock II
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
随机推荐
- Python归纳 | WSGI协议
1.WSGI介绍 1.1什么是WSGI 1.2怎么实现WSGI 2.由Django框架分析WSGI 3.实际环境使用的wsgi服务器 4.WSGI服务器比较
- sql server取某个时间段内所有日期或者所有月份
取所有月份: declare @begin datetime,@end datetime set @begin='2015-2-6' set @end='2015-12-2' declare @mon ...
- PaymentServlet代码
package ${enclosing_package}; import java.io.IOException; import java.util.ResourceBundle; import ja ...
- condition实现原理
condition是对线程进行控制管理的接口,具体实现是AQS的一个内部类ConditionObject,主要功能是控制线程的启/停(这么说并不严格,还要有锁的竞争排队). condition主要方法 ...
- 10个重要的算法C语言实现源代码
包括拉格朗日,牛顿插值,高斯,龙贝格,牛顿迭代,牛顿-科特斯,雅克比,秦九昭,幂法,高斯塞德尔 .都是经典的数学算法,希望能开托您的思路.转自kunli.info 1.拉格朗日插值多项式 ,用于离散数 ...
- 关于myeclipse加载building workspace卡顿的解决办法
在MyEclipse的使用中,在建立新文件或者改动代码后,经常会出现building workspace半天卡顿不动的情况,如果开的程序过多,经常会发生失去响应,电脑要是再烂点,直接死机的情况也常有发 ...
- lua默认是double类型
把c#的float类型传给lua ,lua自己换转成double ,一转就出精度问题 lua只有double没有float ===================================== ...
- 从外网GitHub clone开源项目的时候,.git文件过大,导致克隆慢
以clone impala为例,主要是加入-depth=1参数: git clone -b cdh4-2.0 --depth=1 https://github.com/cloudera/Impala. ...
- cut、grep和排序命令
1.cut 对于行进行操作 cut -d ':' -f 2 以':'为分隔符,切出第二部分的所有行 cut -c 12- 从第12字符往后的字符所有行 2.grep grep '选取的串' 选出所有含 ...
- 实现把dgv里的数据完整的复制到一张内存表
/// <summary> /// 方法实现把dgv里的数据完整的复制到一张内存表 /// </summary> /// <param name="dgv&qu ...