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. subsets 回溯 给定集合,枚举子集。元素不重复

    这个回溯感觉掌握的有些熟练了. 两种方式,递归和循环. 感觉就是套框架了. /** * Return an array of arrays of size *returnSize. * The siz ...

  2. Django 数据库的迁移

    先数据库迁移的两大命令: python manage.py makemigrations & python manage.py migrate 前者是将model层转为迁移文件migratio ...

  3. web前端安全的三个关键点

    一.浏览器的同源策略 同源策略:不同域的客户端脚本在未经授权的情况下不能读写对方的资源. 这里有几个关键词:域.脚本.授权.读写.资源 1.同域要求两个站点:同协议.同域名.同端口.下表展示了所列站点 ...

  4. centos7,进程最大打开文件数 too many open files错误

    遇到一问题,tomcat最近发生几次异常,查看日志,发现一直报 too many open files,熟悉的同学都知道这是用户打开文件数过多导致的, 再用命令ls /proc/20861/fd/ | ...

  5. LeetCode OJ 2. Add Two Numbers

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  6. css3实现文本渐变

    .gradient-text-one{     background-image:-webkit-linear-gradient(bottom, #00d5fd,#84eaff,#00d5fd); - ...

  7. Git设置/取消代理

    设置代理 git config --global http.proxy http://proxy.com:1234 git config --global https.proxy http://pro ...

  8. JSP页面中的小知识

    1.<%…%>和<%!…%>的区别? <%…%>用于在JSP页面中嵌入Java脚本,即代码块 <%!…%>用于在JSP页面中申明变量或方法,可以在该页面 ...

  9. Arcgis Runtime 100.3开发实例源代码调试日志

    Arcgis Runtime 100.3开发实例源代码调试日志 路径: "D:\arcgis runtime1003\arcgis-runtime-samples-dotnet-master ...

  10. Android源码50例汇总,欢迎各位下载(转载)

    下载中心好资料很多,藏在各个角落,小弟在此帮大家做了一个整理,做了一个下载目录,方便大家选择性下载. 源码实例如下: <Android应用开发揭秘>源代码推荐 http://down.51 ...