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

题目大意:

类似I,只不过现在允许进行多次交易,只需在上升曲线累加收益即可,即所谓的贪婪算法。

(贪婪算法与动态规划的区别:贪婪算法只需在每一步求出最大收益,即可在最后一步得到总的最大利润,而对于动态规划,每一步的最大收益可能受到前面某一步的影响,导致必须在最后一步综合前面的状态才能得出最大利润)

Java解法:

public class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0)
return 0;
int profit = 0;
int temp = prices[0];
for(int i = 1; i<prices.length; i++){
if(prices[i] > temp) //上升曲线每步累加收益
profit += prices[i] - temp;
temp = prices[i];
}
return profit;
}
}

(greedy)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. 手游架构-REST架构

    REST架构风格是全新的针对Web应用的开发风格,是当今世界最成功的互联网超媒体分布式系统架构,它使得人们真正理解了Http协议本来面貌.随着 REST架构成为主流技术,一种全新的互联网网络应用开发的 ...

  2. 对list集合去重操作

    import java.util.ArrayList; import java.util.List; //删除集合中重复的数据 public class RemoteTheSameDataInList ...

  3. Android设备中实现Orientation Sensor(图)兼谈陀螺仪

    设备中的三自由度Orientation Sensor就是一个可以识别设备相对于地面,绕x.y.z轴转动角度的感应器(自己的理解,不够严谨).智能手机,平板电脑有了它,可以实现很多好玩的应用,比如说指南 ...

  4. Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1) 错误 解决方案(android-ndk)

    在android里做ndk编程的时候,碰到个随机性错误 错误信息如下: 05-06 15:59:44.411: A/libc(3347): Fatal signal 11 (SIGSEGV) at 0 ...

  5. nexus4/5/6/7/9/10设备谷歌安卓5.1.1系统底包下载

    https://developers.google.com/android/nexus/images http://www.inexus.co/thread-18488-1-1.html

  6. Loadrunner结果分析Graphs

    Transactions(用户事务分析)----用户事务分析是站在用户角度进行的基础性能分析. Transation Sunmmary(事务综述)----对事务进行综合分析是性能分析的第一步,通过分析 ...

  7. ASP.NET静态页生成方法(模板替换)

    本文实例讲述了ASP.NET静态页生成方法的一种简单方法,就是替换内容法. 适用场景 模板比较固定,页面替换内容较少. 基本原理 此方法中静态页生成用到的就是匹配跟替换了,首先得读取模板页的html内 ...

  8. DOS环境下MySQL使用及不同字符集之间的转换

    mysql -uroot -p; show databses; 创建数据库\c; create database webclass; use webclass; 创建表并设置好各字段的属性\c cre ...

  9. C#。4.1数组的应用

    数组的应用 (一).冒泡排序.1.冒泡排序是用双层循环解决.外层循环的是趟数,里层循环的是次数.2.趟数=n-1:次数=n-趟数.3.里层循环使用if比较相临的两个数的大小,进行数值交换. 代码 in ...

  10. (转)【已解决】关于SQL2008 “不允许保存更改。您所做的更改要求删除并重新创建以下表。您对无法重新创建的标进行了更改或者启用了‘阻止保存要求重新创建表的更改’” 解决方案

    近日在使用sql2008的过程中,要对已经创建完成的表结构进行修改,却一直提示弹出如下提示: “ 不允许保存更改.您所做的更改要求删除并重新创建以下表.您对无法重新创建的标进行了更改或者启用了“阻止保 ...