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.

题目

和之前一样,不过你可以买卖任意多次。

思路

相当于从专门做短线操作(short-term operation) , 只要第二天相对前一天有上涨,就transaction

if previous price > current price,   do transaction

profit += previous price - current price

代码

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

[leetcode]122. Best Time to Buy and Sell Stock II 最佳炒股时机之二的更多相关文章

  1. [leetcode]123. Best Time to Buy and Sell Stock III 最佳炒股时机之三

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

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

  3. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  4. LeetCode 122. Best Time to Buy and Sell Stock II (stock problem)

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

  6. leetcode 122. Best Time to Buy and Sell Stock II ----- java

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  7. Java [Leetcode 122]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 ...

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

  9. Java for LeetCode 122 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 ...

随机推荐

  1. vue:在router里面给页面加title

    vue中给组件页面加页面标题:{ path: '/', name: 'index', component: disconnect, meta: { title: '首页' } }, { path: ' ...

  2. myeclipse提示错误。

    Syntax error, parameterized types are only available if source level is 1.5 解决方法:编译器问题.注意myeclipse10 ...

  3. 公网Ip和私网ip

    IP可以分为Public IP 和 Private IP,出现这种规划的原因在于IPv4所能表示的IP太少而电脑太多以至于不够用,然而只有Public IP才能直接连接上网络,所以对于那些公司,学校, ...

  4. python的解构

    今天学习python看到python的解构,觉得很有用就写下来,防止自己忘了 首先定义个列表 然后我们来解构 字典呢?字典需要两个*号才能解构 这样调用不明显 来个明显点的 上面错误是,你定义了一个形 ...

  5. [CI]CodeIgniter应用配置明细

    ---------------------------------------------------------------------------------------------------- ...

  6. 【转】ubuntu 打开命令行窗口的方法

    1. CTRL+ALT+T 2. ALT+F2调出Run a Command,输入gnome-terminal 3. 单击dash home这个按钮,输入te,就可以看到Terminal终端的选项了

  7. Structs复习 开始 第一个helloworld项目

    大体已经学完ssh了  感觉一起做一个项目有点难 计划先用一下独立的Structs 然后再把数据库操作换成hibernate  然后在用Spring 整合 计划用10天左右吧 但今天开始用Struct ...

  8. eclipse maven项目 热部署

    热部署:本地项目一键发布到远程服务器中 热部署步骤: 1. 在tomat/conf/tomcat-users.xml添加 <role rolename="manager-gui&quo ...

  9. 解决:python 连接Oracle 11g 报错:ORA-12514: TNS: 监听程序当前无法识别连接描述符中请求的服务

    其次,将查询到的service_name替换sid即可:conn=cx_Oracle.connect('hr/admin@localhost:1521/EE.oracle.docker')

  10. tf.Variable和tensor的区别(转)

    刷课过程中思考到Variable和Tensor之间的区别,尝试发现在如下代码中: a = tf.Variable(tf.ones(1)) b = tf.add(a,tf.ones(1)) 1 2 a是 ...