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

解题思路:

仿照上题,如果遍历中出现下降,即卖掉同时将buy设置为下降点,JAVA实现如下:

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

Java for LeetCode 122 Best Time to Buy and Sell Stock II的更多相关文章

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

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

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

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

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

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

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

  9. LeetCode 122 Best Time to Buy and Sell Stock II(股票买入卖出的最佳时间 II)

    翻译 话说你有一个数组,当中第i个元素表示第i天的股票价格. 设计一个算法以找到最大利润. 你能够尽可能多的进行交易(比如.多次买入卖出股票). 然而,你不能在同一时间来多次交易. (比如.你必须在下 ...

随机推荐

  1. nginx 启动,重启,添加开机启动等相关命令

    nginx -t 测试 配置文件是否正确,同时可以查看配置文件路径 nginx -c /usr/local/nginx/conf/nginx.conf   启动nginx ps -ef|grep ng ...

  2. 使用 ODP.NET 访问 Oracle(.net如何访问Oracle)详解【转】

    http://www.cnblogs.com/qinpengming/archive/2013/06/08/3127346.html 1,什么是ODF .NE,?就是Oracle 为 .NET (OD ...

  3. linux中read用法

    read在while中的经常用法: root@ubuntu:/var/lib/logrotate :: # cat /etc/cron.daily/logrotate #!/bin/sh # Clea ...

  4. 关于阿里 weex 的使用与案例

    1. 阿里宣布开源Weex http://mp.weixin.qq.com/s?__biz=MzA4MjA0MTc4NQ==&mid=504089541&idx=1&sn=3a ...

  5. OCP-1Z0-051-题目解析-第16题

    16. Evaluate the following query: SQL> SELECT promo_name q'{'s start date was }' promo_begin_date ...

  6. TP框架中多条件筛选

            $pid =I('pid');         $year = I('year');         $productType = I('productType');         ...

  7. 循环时的dom操作

    <body> <div id="resText"></div> <div id="reshtml"></d ...

  8. 【ORACLE】ORA-27102: out of memory报错的处理

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...

  9. html5 cocos2d js Access-Control-Allow-Origin

    1.No 'Access-Control-Allow-Origin' header is present on the requested 近期在接html5的渠道,遇到了跨域的问题,使用 js 的 ...

  10. java中url正则regex匹配

    String regex = "^(?:https?://)?[\\w]{1,}(?:\\.?[\\w]{1,})+[\\w-_/?&=#%:]*$"; 解释说明: ^ : ...