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 (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

Input: [7,1,5,3,6,4]
Output: 7
Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
  Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

思路:

如果可以交易多次,求利润最大,那么,只要第二天比前一天价格高就卖出。没一个value -> peak 都是一次盈利。

注意:

for循环 i < prices.length -1  注意要-1!因为比较的是 prices[i+1]. 如果i < prices.length, 会出现outOfIndex错误。

代码:

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

Leetcode122-Best Time to Buy and Sell Stock II-Easy的更多相关文章

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

  2. LeetCode122——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 a ...

  3. LeetCode122: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 a ...

  4. Leetcode No.122 Best Time to Buy and Sell Stock II Easy(c++实现)

    1. 题目 1.1 英文题目 You are given an array prices where prices[i] is the price of a given stock on the it ...

  5. LeetCode_122. Best Time to Buy and Sell Stock II

    122. Best Time to Buy and Sell Stock II Easy Say you have an array for which the ith element is the ...

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

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

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

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

  10. 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. flask实战-个人博客-使用蓝本模块化程序

    使用蓝本模块化程序 实例化flask提供的blueprint类就创建一个蓝本实例.像程序实例一样,我们可以为蓝本实例注册路由.错误处理函数.上下文处理函数,请求处理函数,甚至是单独的静态文件文件夹和模 ...

  2. 转:【专题五】TCP编程

    前言 前面专题的例子都是基于应用层上的HTTP协议的介绍, 现在本专题来介绍下传输层协议——TCP协议,主要介绍下TCP协议的工作过程和基于TCP协议的一个简单的通信程序,下面就开始本专题的正文了. ...

  3. java中避免乱码

    response.setContentType("text/html;charset=UTF-8"); 这个是在action中的 这个是在json中设置乱码的 contentTyp ...

  4. Ford VCM II Ford VCM2 Diagnostic Tool with Ford IDS v108 Installed On Laptop Ready to Use

    HOW to VCM2 Ford VCM II with Ford IDS v108 Work Well? VCM2 Ford VCM2 Ford diagnostic tool hot sale i ...

  5. 第一周java测验感想

     在正式开学的第一周,建民老师就给我们来了一个下马威.我本身的编程基础比较差,不知道怎么去想,怎么去一步步的去完成这么一个工程.所以我在星期四的下午十分的痛苦…因为不知道怎么搞嘛.尽管在暑假的时候看了 ...

  6. 远程获得乐趣的 Linux 命令

    今天是我们为期 24 天的 Linux 命令行玩具日历的最后一天.希望你一直有在看,但如果没有,请从头开始,继续努力.你会发现 Linux 终端有很多游戏.消遣和奇怪之处. 虽然你之前可能已经看过我们 ...

  7. GoldenGate实时投递数据到大数据平台(4)- ElasticSearch 2.x

    ES 2.x ES 2.x安装 下载elasticSearch 2.4.5, https://www.elastic.co/downloads/elasticsearch 解压下载后的压缩包,启动ES ...

  8. The Little Prince-11/28

    The Little Prince-11/28 Today I find some beautiful words from the book. You know -- one loves the s ...

  9. MySQL半同步安装以及参数

    MySQL半同步安装以及参数 基于MySQL5.5 官档地址: Semisynchronous Replication Administrative Interface https://dev.mys ...

  10. redis 数据统计(用自增id防止同一秒并发过大没统计成功)

    Redis 缓存保存某段时间累加的数值,加入最大id防止同一秒并发过大,导致只统计了执行时同一秒的部分数据,而同一秒另一部分数据在下次累加时没有统计到缓存中 //coin总数 public funct ...