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. MySQL超时配置

    connect_timeout:连接响应超时时间.服务器端在这个时间内如未连接成功,则会返回连接失败. wait_timeout:连接空闲超时时间.与服务器端无交互状态的连接,直到被服务器端强制关闭而 ...

  2. 设计模式之Factory(工厂)(转)

    定义:提供创建对象的接口. 为何使用? 工厂模式是我们最常用的模式了,著名的Jive论坛 ,就大量使用了工厂模式,工厂模式在Java程序系统可以说是随处可见. 为什么工厂模式是如此常用?因为工厂模式就 ...

  3. Njinx配置

    参考地址: NGINX的百度百科:https://baike.baidu.com/item/nginx/3817705?fr=aladdin NGINX的中文网站:http://www.nginx.c ...

  4. SQL8数据库定期自动备份

    我们知道,利用SQL Server 2008数据库可以实现数据库的定期自动备份.方法是用SQL SERVER 2008自带的维护计划创建一个计划对数据库进行备份, 下面我们将SQL SERVER 20 ...

  5. 新建web项目myeclipse基本设置

    1. General --> Workspace --> UTF-82. General --> Editors --> Associations --> JSP --& ...

  6. right spindle supply short to gnd

    hardware guy found that the R1004 lead to this error, but this error should not be checked, because ...

  7. python pip

    如果pip的版本较低,可能导致pip时安装出错,所以我们要更新pip版本-- 查询pip版本 pip -V -- Linux and OS X 升级 pip install -U pip -- Win ...

  8. Python查看关键字和帮助信息

    1.查看所有的关键字 >>> help('keywords') Here is a list of the Python keywords. Enter any keyword to ...

  9. VScode 好用插件集合(一)

    VScode 好用插件集合(一) 什么是VScode Visual Studio Code (简称 VS Code / VSC) 是一款免费开源的现代化轻量级代码编辑器,支持几乎所有主流的开发语言的语 ...

  10. Android - Resource 之 Menu 小结

    定义一个application的菜单,由MenuInflater召唤. 位置: res/menu/filename.xml 类型:指向Menu resource 文法: <?xml versio ...