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

题目大意:跟上一题类似,都是求最大获利,上一题要求只能买卖一次,这次可以多次买卖。

解题思路:从第2天开始,只要是比前一天价格低就不管,比前一天价格高就卖出。

  1. public int maxProfit(int[] prices) {
  2. if (prices == null || prices.length == 0) {
  3. return 0;
  4. }
  5. int min_pos = 0;
  6. int max_pro = 0;
  7. for (int i = 1; i < prices.length; i++) {
  8. if (prices[i] > prices[i - 1]) {
  9. max_pro += prices[i] - prices[min_pos];
  10. }
  11. min_pos = i;
  12. }
  13. return max_pro;
  14. }

Best Time to Buy and Sell Stock II ——LeetCode的更多相关文章

  1. Best Time to Buy and Sell Stock II leetcode java

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...

  2. 122. Best Time to Buy and Sell Stock II ——LeetCode

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  3. Best Time to Buy and Sell Stock II [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ Basic idea: ...

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

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

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

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

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

随机推荐

  1. PHP简单利用token防止表单重复提交(转)

    <?php/* * PHP简单利用token防止表单重复提交 */function set_token() { $_SESSION['token'] = md5(microtime(true)) ...

  2. Qt深入:不能不知道的Type、Attribute和Flags

    Qter高手与新手的区别在于:知道还是不知道 Qt不是开发语言,所以无所谓谁厉害.但使用他的Qter却有着差异,也许是面向对象语言本身的.或者实际经验上的.而对于Qt本身来说,高手与新手最显著的差异在 ...

  3. win7/win8 64位系统注册TeeChart8.ocx 控件---以及dllregisterserver调用失败问题解决办法

    TeeChart控件就不多介绍了,很多朋友不知道开始怎么注册使用,尤其是在64位系统下如何注册的问题,具体如下: win7.win8  64位系统问题所在: 64位的系统一般都是可以安装32位程序的 ...

  4. ASP.NET面试

    1.net中读写数据库需要用到那些类?他们的作用都是什么?答:DataSet:数据存储器.DataCommand:执行语句命令.DataAdapter:数据的集合,用语填充.2.介绍一下什么是Code ...

  5. jsp页面可以巧用模态框

    jsp页面使用模态框配合ajax出来的效果真的没话说,当然你也可以使用模态框配合action,但是在删除和更新的时候传值有点麻烦,用ajax 就没有这些问题 ,比如删除代码的时候在js文件中传值可以这 ...

  6. iOS真机测试种可能遇到的问题

    1. Reason- image not found 用模拟器是没有问题的,不过在真机好像是有问题,不确定是否是所有机型. 崩溃日志   1 2 3 4 5   dyld: Library not l ...

  7. wpf 控件复制 克隆

    方法1: string xaml = System.Windows.Markup.XamlWriter.Save(rtb1); RichTextBox rtb2 =System.Windows.Mar ...

  8. 通过dbcp链接池对数据库操作报 Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)--解决方案

    org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for ...

  9. 试用ubuntu-12.04.3-desktop-amd64(二)

    首先说明,采用主机+虚拟机+ubuntu的形式,更具体的则为Win7-64bit + VMWare + ubuntu-12.04.3-desktop-amd64 进入ubuntu后首先考虑到的就是怎么 ...

  10. 一元云购完整源码 云购CMS系统 带安卓和ios手机客户端

    看起来不错的一套一元云购CMS源码,源码包里面带了安卓和ios手机客户端,手机客户端需要自己反编译.    这里不做功能和其它更多的介绍,可以自己下载后慢慢测试了解.    下面演示图为亲测截图< ...