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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...
随机推荐
- Hdu 4280 Island Transport(最大流)
Island Transport Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- SpringMVC-Interceptor拦截Session登录
背景: 开发的项目都须要账号password登录才干够查看站点的内容,所以我们设计时须要考虑,用户进入站点仅仅能从一个我们设计的规范通道进入即通过注冊的账号password登录,其它方法都是非法的和不 ...
- Swift String转Character数组
通过String的characters方法,将String转Character数组 例如: let characters:Array<Character> = Array("01 ...
- 使用记事本开发第一个java程序
记事本是开发java程序最基础的一个工具 第一步:编写 新建一个文件名为Hello的文本文档,并将文件扩展名改为.java. 在文本框内输入我们的程序代码 ——————————————我是分割线——— ...
- 卷积操作中的矩阵乘法(gemm)—— 为什么矩阵乘法是深度学习的核心所在
1. 全连接 k 个输入: n 个神经元: 每个神经元都会学到一组权值向量,以和输入进行内积运算: n 个输出: 2. 卷积 卷积操作对于高维(多个平面)的输入,单个卷积核的深度应和输入的深度(dep ...
- JSTL中的常用EL函数(fn:contains(str,subStr))
转自:https://blog.csdn.net/u012843873/article/details/53289238 ① fn:toLowerCase ④fn:length fn:length函数 ...
- 反向Shell增强
下载socat 在客户端: socat file:`tty`,raw,echo=0 tcp-listen:4444 在服务端: socat exec:'bash -li',pty,stderr,set ...
- github结合TortoiseGit使用sshkey,无需每次输入账号和密码
首先需要明确,github上支持三种方式进行项目的clone https,ssh,subversion ssh的方式 git@github.com:用户名/版本库t.git ...
- ifame子页实现父页面刷新(或跳转到指定页面)
<script>parent.location.replace('../D_DailyManager/Add.aspx?id=" + x + "');</scri ...
- Android Finalizing a Cursor that has not been deactivated or closed
问题描述: 使用Sqlite数据库时,有时候会报下面的异常: Finalizing a Cursor that has not been deactivated or closed 一个光标没有被停用 ...