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天开始,只要是比前一天价格低就不管,比前一天价格高就卖出。

    public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int min_pos = 0;
int max_pro = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
max_pro += prices[i] - prices[min_pos];
}
min_pos = i;
}
return max_pro;
}

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. sbt 配置

    1. SBT使用local maven repository,下载的库依然放在 ~/.m2/repository 2. SBT assembly 会把依赖库打包到一个jar包,需要使用assembly ...

  2. C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用

    //注意:请使用VS2010打开以下的源代码. //源代码地址:http://pan.baidu.com/s/1j9WVR using System; using System.Collections ...

  3. Linux FTP YUM源搭建简单记录

    1. 挂载iso镜像,进入Packages目录下安装FTP服务 #rpm -ivh vsftpd-3.0.2-10.el7.x86_64.rpm 2. 修改vsftpd配置文件如下 [root@rus ...

  4. Android中SQLite使用

    现在的主流移动设备像Android.iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储我们大量的数据,所以我们就需要掌握移动设备上 ...

  5. HTML 学习笔记

    1HTML 标题(Heading)是通过 <h1> - <h6> 等标签进行定义的. 并且只有这6种标题,标题中加多个空格,和一个空格没区别,标题文字前后加默认空格会被去除. ...

  6. SQL Server 存储过程分页

    每每面试,总会有公司问到分页.在下不才,在这里写几种分页,望路过的各位大神尽情拍砖. 先从创建数据库说起.源码如下 一.创建数据库 /********************************* ...

  7. ASP.NET页面错误处理

    ASP.NET 提供三种用于在出现错误时捕获和响应错误的主要方法:Page_Error 事件.Application_Error 事件以及应用程序配置文件 (Web.config). 这里主要介绍Ap ...

  8. jquery get checkbox inside element(td).

    <td id="skill"><input name="skill" type="checkbox" value=&quo ...

  9. 安装php时,make test报错

    Build complete. Don't forget to run 'make test'. [root@localhost php-5.4.5]# make test ERROR: Cannot ...

  10. Oracle11G 数据库 expdp、impdp使用示例

    expdp 备份数据库.expdp/impdp简单测试 操作系统层面创建目录 [root@Oracle11g ~]# mkdir -p /home/oracle/db_back/ 修改目录的所属用户. ...