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. [转]使用Wireshark来检测一次HTTP连接过程

    Wireshark是一个类似tcpdump的嗅探软件,界面更人性化一些,今天我用它来检测一次HTTP连接过程. 安装好之后,先配置一下,选择Capture->Options,先设定你要嗅探的网络 ...

  2. C# 基础练习题

    (一)循环 1.打印输出 5 个"Hello world!" ////输出 5 个"Hello world!" for (int i = 0; i < 5 ...

  3. Scut游戏服务器引擎5.6.3.5发布

    版本:5.6.3.5 (2013-11-25) 1. 优化实体ChangeKey队列,减少写库IO(默认为5分钟写入一次数据库) 2. 优化Protobuf序列化启用自动GZip压缩,减少Redis内 ...

  4. DotnetBrowser入门教程-入门

    在.net core时代,web开发基本可以用.net core 2.0取代了.但是在传统领域,桌面开发仍然是不可以抛弃的,譬如: 1.用户需要和串口或者硬件打交道. 2.用户只想简单的安装好就使用, ...

  5. C#数据之DataTable

    C#创建DataTable的几种方式 第一种方式:直接添加数据对象 DataTable table = new DataTable(); table.Columns.Add("strName ...

  6. bat文件转换为exe文件

    批处理文件转换为exe文件(简单的处理文件),点击下载 使用超简单的了,不多说.

  7. 编译-O 选项对性能提升作用

    GCC -O 选项 这个选项控制所有的优化等级.使用优化选项会使编译过程耗费更多的时间,并且占用更多的内存,尤其是在提高优化等级的时候. -O设置一共有五种:-O0.-O1.-O2.-O3和-Os. ...

  8. http url转义字符,特殊字符

    空格 - %20 " - %22 # - %23 % - %25 & - %26 ( - %28 ) - %29 + - %2B , - %2C / - %2F : - %3A ; ...

  9. spring-web中的WebDataBinder理解

    Spring可以自动封装Bean,也就是说前台通过SpringMVC传递过来的属性值会自动对应到对象中的属性并封装成javaBean,但是只能是基本数据类型(int,String等).如果传递过来的是 ...

  10. ubuntu16.04下Cmake学习一

    根据网上的资料,我总结了一下,一个工程应该有根目录(bin)存放可执行文件,头文件目录(include)存放头文件,源码文件(src)存放你的算法,还需要一个库文件夹存放你编译的静态库或者动态库.然后 ...