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 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).
Subscribe to see which companies asked this question
分析
买卖股票II,与LeetCode(121) Best Time to Buy and Sell Stock的区别就是,此次允许买卖多次,但是必须卖出后才可下次买入。
实际上是求股价波浪线的所有上升段的长度和。
AC代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
if (prices.empty())
return 0;
int sum = 0;
//只要后项比前项大,这部分值就一定可以变为利润
for (size_t i = 1; i < prices.size(); ++i)
{
if (prices[i] > prices[i - 1])
sum += (prices[i] - prices[i - 1]);
}
return sum;
}
};
LeetCode(122) Best Time to Buy and Sell Stock II的更多相关文章
- LeetCode(123) Best Time to Buy and Sell Stock III
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(121) Best Time to Buy and Sell Stock
题目 Say you have an array for which the ith element is the price of a given stock on day i. If you we ...
- leetcode 121 122 123 . Best Time to Buy and Sell Stock
121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...
- 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(java)解答
转载请注明出处:z_zhaojun的博客 原文地址 题目地址 Best Time to Buy and Sell Stock II Say you have an array for which th ...
- 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 ...
- 31. leetcode 122. Best Time to Buy and Sell Stock II
122. Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price ...
- leetcode 121. Best Time to Buy and Sell Stock 、122.Best Time to Buy and Sell Stock II 、309. Best Time to Buy and Sell Stock with Cooldown
121. Best Time to Buy and Sell Stock 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
- 【LEETCODE】37、122题,Best Time to Buy and Sell Stock II
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- FusionCharts图表控件中文版使用手册
三要素:swf.data.xml.承载图表的载体 1.Swf: 按照你所设计的图表类型加载相应的.swf文件到你的工程即可(eg:若你想生成一张二维柱状图,那么在你的工程里就必须包含Column2D. ...
- 076 Minimum Window Substring 最小窗口子字符串
给定一个字符串 S 和一个字符串 T,找到 S 中的最小窗口,它将包含复杂度为 O(n) 的 T 中的所有字符.示例:S = "ADOBECODEBANC"T = "AB ...
- 洛谷 P1821 [USACO07FEB]银牛派对Silver Cow Party
银牛派对 正向建图+反向建图, 两边跑dijkstra,然后将结果相加即可. 反向建图以及双向建图的做法是学习图论的必备思想. #include <iostream> #include & ...
- CRC检错技术原理
一.题外话 说来惭愧,一开始是考虑写关于CRC检错技术更深层次数学原理的,然而在翻看<Basic Algebra>后,我果断放弃了这种不切实际的想法.个人觉得不是因为本人数学水平差或者能力 ...
- Beginning Python Chapter 1 Notes
James Payne(American)编写的<Beginning Python>中文译作<Python入门经典>,堪称是Python的经典著作. 当然安装Python是很简 ...
- Spark 配置整理
Spark 的配置有很多,这里一方面总结一下官方文档中的内容,一方面将网上查到的资料中用到的针对特定问题的配置整理一下. 先看一下官网的配置:http://spark.apache.org/docs/ ...
- 关系代数演算So Easy
关系代数运算So Easy 关系代数是以关系为运算的一组高级运算的集合.由于定义为属性个数 相同的元组的集合,因此集合代数的操作就可以引入到关系代数中.关系代数也可以看做是一种抽象的查询语言,是对关系 ...
- ACM博弈论基础
博弈论的题目有如下特点: 有两名选手 两名选手交替操作,每次一步,每步都在有限的合法集合中选取一种进行 在任何情况下,合法操作只取决于情况本身,与选手无关 游戏败北的条件为:当某位选手需要进行操作时, ...
- Python-OpenCV中的resize()函数
改变图像大小意味着改变尺寸,无论是单独的高或宽,还是两者.也可以按比例调整图像大小. 这里将介绍resize()函数的语法及实例. 语法 函数原型 cv2.resize(src, dsize[, ds ...
- python 与 json
+-------------------+---------------+ | Python | JSON | +================= ...