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的更多相关文章

  1. [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 ...

  2. 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 ...

  3. 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- ...

  4. 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 ...

  5. 【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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 题目的要求是只买卖一次,买的价格越低,卖的价格越高,肯定收益就越大 遍历整个数组,维护一个当前位置之前最低的买入价格,然后每次 ...

随机推荐

  1. Mybatis分页插件2.0版本号公布

    项目地址:http://git.oschina.net/free/Mybatis_PageHelper 软件介绍:http://www.oschina.net/p/mybatis_pagehelper ...

  2. nyoj 119 士兵杀敌(三) 【线段树】【单点更新】

    题意:. .. 策略如题. 思路:我们先如果仅仅求某一区间的最大值.我们仅仅须要利用线段树的模板.仅仅须要初始化和询问的时候小小的改动一下.改成祖先结点储存的不再是子节点的和而是两个子节点之间的最大值 ...

  3. 折腾开源WRT的AC无线路由之路-5

    -在Mac上设置无password连接SSH 1. 生成SSH密钥对 <pre name="code" class="html">ssh-keyge ...

  4. 升级Xcode8后的相机crash问题-IOS10权限问题

    当我升级到Xcode8后,启动我的相机项目,直接crash,输出的日志如下: '2016-07-08 16:41:11.268943 project-name[362:56625] [MC] Syst ...

  5. 英语发音规则---X字母

    英语发音规则---X字母 一.总结 一句话总结: 1.x位于词尾或音节尾部,读/ks/? box /bɒks/ n.盒; 箱状物 fix /fɪks/ vt.固定 fox /fɒks/ n.狐; 狐狸 ...

  6. 初涉springboot

    1.首先,我们需要了解微服务是什么? 微服务 (Microservices) 是一种软件架构风格,它是以专注于单一责任与功能的小型功能区块 (Small Building Blocks) 为基础,利用 ...

  7. Tomcat 初探(二) server.xml 配置

    前言 在上一篇文章中,我们在示例中演示了网站的发布,其中涉及到了 server.xml 的修改,本文中我会给大家详细解释一下 server.xml 文件中的节点及其属性的作用,本片文章参考并摘抄了他人 ...

  8. Winform WPF 窗体显示位置

    WinForm 窗体显示位置 窗体显示的位置首先由窗体的StartPosition决定,FormStartPosition这个枚举值由如下几种情况 // 摘要: // 窗体的位置由 System.Wi ...

  9. sql server 启用数据库的 Service Broker

    使用SqlDependency时要开启Service Broker ,那么SqlDependency是什么 https://www.cnblogs.com/zhaoyihao/p/5663258.ht ...

  10. mobiscroll插件的基本使用方法

    前一阵子接触到了mobiscroll插件,用在移动端的日期选择上,感觉倍棒,于是便敲了一个小案例,与大家一起分享分享 <!DOCTYPE html> <html lang=" ...