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. linux mount命令学习

    我们在下面这篇博文中已经有笼统的学习了文件系统的一些相关知识, http://blog.csdn.net/boyxulin1986/article/details/12107113 本篇我们主要是用来 ...

  2. 【C#正则基础】正则表达式

    1. 代表任意多个字符:(.*?)2. 代表网页里的<body>*</body>任意的标签内容,替换以后网页源码就只剩纯文本:<[^>]*>3. 代表网页中的 ...

  3. Node.js 博客实例(六)留言功能

    原教程https://github.com/nswbmw/N-blog/wiki/_pages的第六章,因为版本号等的原因,在原教程基础上稍加修改就可以实现. 实现用户给文章留言的功能,留言保存在数据 ...

  4. android 多项对话框

    在main.xml中 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  5. Python enumerate函数

    enumerate函数接受一个可遍历的对象,如列表.字符串,可同时遍历下标(index)及元素值(value) >>> a = ['aaa','bbb','ccc',1235] &g ...

  6. [Python爬虫笔记][随意找个博客入门(一)]

    [Python爬虫笔记][随意找个博客入门(一)] 标签(空格分隔): Python 爬虫 2016年暑假 来源博客:挣脱不足与蒙昧 1.简单的爬取特定url的html代码 import urllib ...

  7. 开发日志_Jan.9

    今天主要工作为修改昨天的碰撞引擎不符合预期的部分. 经过了昨天的工作,碰撞算法已经初见雏形.但是主要有两个问题: 碰撞反弹的方向与预期不符合 碰撞后球与机器人存在一个"黏在一起"的 ...

  8. ubantu命令安装banner

    banner命令可以输出图形字符 在线yum安装 $ sudo apt-get update;sudo apt-get install sysvbanner

  9. Chrome Browser

    set default search engine as follow for force encrypted searching: https://encrypted.google.com/sear ...

  10. SQL2008安装重启失败

    我今天安装SQL2008的一些问题经历SQL2008安装重启失败大致出错信息如下:RebootRequiredCheck 检查是否需要挂起计算机重新启动.挂起重新启动会导致安装程序失败. 失败 需要重 ...