1. Say you have an array for which the ith element is the price of a given stock on day i.
  2.  
  3. 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. 而且,对于一个上升子序列,比如:512340 中的1234序列来说,对于两种操作方案:
  3. 一,在1买入,4卖出;
  4. 二,在1买入,2卖出同时买入,3卖出同时买入,4卖出;
  5. 这两种操作下,收益是一样的。
  6.  
  7. 所以算法就是:从头到尾扫描prices,如果ii-1大,那么price[i] price[i-1]就可以计入最后的收益中。这样扫描一次O(n)就可以获得最大收益了。
  8.  
  9. 摘自: http://blog.unieagle.net/2012/12/04/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Abest-time-to-buy-and-sell-stock-ii/

  

  1. class Solution {
  2. public:
  3. int maxProfit(vector<int> &prices) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. int len = prices.size();
  7. if(len < ) return ;
  8.  
  9. int res = ;
  10. for(int i = ; i< len ; ++i)
  11. {
  12. if(prices[i] > prices[i-])
  13. res+= prices[i] - prices[i-];
  14. }
  15.  
  16. return res;
  17.  
  18. }
  19. };

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. 通过并行 提高批量审核PDF性能

    上一篇文章提到了 通过 iTextSharp 实现PDF 审核盖章 ,如果当需要一次审核大批量的PDF我们如何来提高程序的性能呢? 下面我们通过并行计算来提升性能. 首先是一个审核PDF的方法 pub ...

  2. ubuntu下,thinkphp验证码不能用

    首先安装gd库 使用apt-get方式为Ubuntu安装PHP+MYSQL+Apache 默认是没有GD库的 1.安装gd库 命令:sudo apt-get install php5-gd 2.给权限 ...

  3. linux ubuntu 浏览器 字 字体 虚 解决办法

    在刚装好的ubuntu上什么都好,只是浏览器字体很虚,就连百度都虚 解决办法很简单 安装雅黑字体 修改font conf sudo vi /etc/fonts/conf.avail/69-langua ...

  4. lesson1:压测普通网页

    本文展示了利用jmeter来压力测试普通网页,具体步骤如下: 1.添加[线程组]“lesson1压测普通网页”,“线程数”设置为10:“循环测试“设置为50,如图所示: 2.添加一个"htt ...

  5. JavaScript学习笔记(高级部分—01)

    JavaScript的核心ECMAScript描述了该语言的语法和基本对象:DOM描述了处理网页内容的方法和接口:BOM描述了与浏览器进行交互的方法和接口. 简单说,ECMAScript描述了以下内容 ...

  6. HDU4612(Warm up)2013多校2-图的边双连通问题(Tarjan算法+树形DP)

    /** 题目大意: 给你一个无向连通图,问加上一条边后得到的图的最少的割边数; 算法思想: 图的边双连通Tarjan算法+树形DP; 即通过Tarjan算法对边双连通缩图,构成一棵树,然后用树形DP求 ...

  7. [A Top-Down Approach][第二章 应用层]

    [A Top-Down Approach][第二章 应用层] 标签(空格分隔): 未分类 网络应用是计算机网络存在的理由 首先从定义几个关键的应用层概念开始 应用程序所需要的网络服务,客户和服务器,进 ...

  8. C复习手记(Day3)

    C预处理器 C 预处理器不是编译器的组成部分,但是它是编译过程中一个单独的步骤.简言之,C 预处理器只不过是一个文本替换工具而已,它们会指示编译器在实际编译之前完成所需的预处理.我们将把 C 预处理器 ...

  9. Ajax客户登陆验证

        服务器端操作方便之处我就不吹了,地球人都知道,它最烦莫过于页面刷新,头都被刷晕了,而且他在刷新的时候,还触发服务器端的事件,现在Ajax的出现,他们的结合是发展的必然!    一.介绍一下Aj ...

  10. Directory.GetFiles 方法

    Directory.GetFiles 方法 返回指定目录中文件的名称(包括其路径). 命名空间:   System.IO程序集:  mscorlib(mscorlib.dll 中) Enumerate ...