110_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 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).
1:注意特殊情况;2:找到数组相邻的凹点和凸点;3:两者的差值是当前的最大值。4:在查找凸凹值的时候注意边界
int maxProfit(vector<int> &prices)
{
if(prices.size() <= 1)
{
return 0;
} int maxValue = 0;
int start = 0;
int end = 0;
int size = (int)prices.size(); while(start < size)
{
while(start < size - 1 && prices[start] >= prices[start + 1])
{
start++;
} end = start + 1;
while(end < size - 1 && prices[end] <= prices[end + 1])
{
end++;
} if(end == size)
{
break;
}
else
{
maxValue += prices[end] - prices[start];
} start = end + 1;
} return maxValue;
}
110_leetcode_Best Time to Buy and sell Stock II的更多相关文章
- [LintCode] 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 —— 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 ...
- 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...
- 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 pric ...
- 【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 ...
- 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: Best Time to Buy and Sell Stock II 解题报告
Best Time to Buy and Sell Stock IIQuestion SolutionSay you have an array for which the ith element i ...
- 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 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- android Service中多线程交互
android 的service和activity是执行在UI主线程的. 在android线程中,仅仅有主线程即UI线程有自己的默认的消息队列.子线程须要创建自己的消息队列.并把消息发给队列,并循环起 ...
- VC与JavaScript交互(一) ———— 怎样实现
为什么要让VC与JavaScript交互? 1.有时候我们须要让自己的软件打开一个网页.来获取页面上的一些数据. 这时,能够用mshtml解析HTML提取出数据.也能够向HTML文档动态写入我们准备好 ...
- HDOJ 2647 Reward 【逆拓扑排序+分层】
题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. ...
- MySQL List分区(三)
具体介绍请看 MySQL分区一 样例:该样例为本人个人学习总结分享
- zzulioj--1637--Happy Thanksgiving Day - WoW yjj!(水)
1637: Happy Thanksgiving Day - WoW yjj! Time Limit: 1 Sec Memory Limit: 128 MB Submit: 104 Solved: ...
- Java中的作用域有哪些
在Java语言中,变量的类型主要有3种:成员变量.静态变量和局部变量 首先说静态变量跟局部变量 静态变量不依赖于特定的实例,而是被所有实例共享,也就是说,只要一个类被加载,JVM就会给类的静态变量分配 ...
- maven、spring jdbc与mysql、mybatis
以它们之前的一个简单用例作为实例. 一个简单的能跑起来的实例.原文网址.非常好的例子. http://www.open-open.com/lib/view/open1390534380648.html ...
- SQL Server 查询所有包含某文本的存储过程、视图、函数
• 方法一:查询所有包含某文本的存储过程.视图.函数 SELECT * from sysobjects o, syscomments s where o.id = s.id AND text LIK ...
- php配置站点
第一步:需要打开三个文件 1.C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf 2.C:\wamp\bin\apache\apache2.4.9\conf\ ...
- php版本过低错误导致的laravel 错误:Illuminate\Foundation\helpers.php on line 233; syntax error, unexpected '?'
今天运行laravel项目发现出现错误: Parse error: syntax error, unexpected '?' ..\vendor\laravel\framework\src\Illu ...