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. linux下查看磁盘空间

    如果要查看磁盘还剩多少空间,当然是用df的命令了. [root@localhost ~]# df -h  文件系统              容量 已用 可用 已用% 挂载点  /dev/sda2   ...

  2. Java中如何分析一个案列---猫狗案例为例

    猫狗案例: 具体事务: 猫.狗 共性: 姓名.年龄.吃饭 分析:从具体到抽象 猫: 姓名.年龄--->成员变量 吃饭       ---> 成员方法 构造方法:无参.有参 狗: 姓名.年龄 ...

  3. Android中px、dp、sp的区别

    px: 即像素,1px代表屏幕上一个物理的像素点: px单位不被建议使用,因为同样100px的图片,在不同手机上显示的实际大小可能不同,如下图所示(图片来自android developer guid ...

  4. ASP.NET中 分析器错误:发现不明确的匹配

    这是一个不好的代码习惯引起的发布后运行时的问题.错误原因为.net2.0无法正确识别服务器控件和变量的大小写区别,但是这个错误只有在iis中体现,在文件系统的调试中没有发生. 错误信息 引发错误的参考 ...

  5. asp.net中@page 指令的属性Inherits、Src、CodeBehind区别

    在 ASP.NET 中使用代码隐藏方法来设计Web 窗体,可使页代码能够更清晰地从 HTML 内容中分离到完全单独的文件中. <%@ Page language="c#" C ...

  6. #BeginLibraryItem 的疑问...

    <!-- #BeginLibraryItem "/library/ur_here.lbi" --><div style="padding:3px 15p ...

  7. c - static 变量

    static变量和普通的局部变量不同,位于数据区中,在函数的外部初始化. ref: http://www.cnblogs.com/hustcat/archive/2009/06/30/1513755. ...

  8. linux术语解析(持续更新)

    1.linux内核有个版本,分别是 longterm: 提供长期支持的内核版本 stable: 稳定版本 Beta 测试版

  9. 实时错误 '91' :对象变量或with块变量未设置

    大家这几天在做学生信息管理系统的时候,出现最多的应该就是这个问题了,“实时错误‘91’:对象变量或with块变量未设置”.如右图: 遇到这个问题,我们首先应该去参考MSDN,不过这时候MSDN似乎没有 ...

  10. Semaphore (通常用于限制可以访问某些资源(物理或逻辑的)的线程数目)

    Semaphore 通常用于限制可以访问某些资源(物理或逻辑的)的线程数目.例如,下面的类使用信号量控制对内容池的访问: 方法详解: 构造方法摘要 Semaphore(int permits)     ...