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 algorithm to find the maximum profit. You may complete at most two transactions.

Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

让我们看看它又耍了什么新花样 还是找最大利润  但是只允许你交易两次 还是在买之前必须把之前买的卖出去

没思路。。  有点棘手。。

能在遍历一遍的情况下得到解吗  感觉可以。。想一会先

怎么感觉这种题好适合股票啊 。。完全没思路

还是维护一个最小值 和 利润值  交易过后重置最小值  同时维护最大的两个利润值?

没思路 去discuss区看看大神怎么解的- -

public class Solution {
public int maxProfit(int[] prices) {
int hold1 = Integer.MIN_VALUE, hold2 = Integer.MIN_VALUE;
int release1 = 0, release2 = 0;
for(int i:prices){ // Assume we only have 0 money at first
release2 = Math.max(release2, hold2+i); // The maximum if we've just sold 2nd stock so far.
hold2 = Math.max(hold2, release1-i); // The maximum if we've just buy 2nd stock so far.
release1 = Math.max(release1, hold1+i); // The maximum if we've just sold 1nd stock so far.
hold1 = Math.max(hold1, -i); // The maximum if we've just buy 1st stock so far.
}
return release2; ///Since release1 is initiated as 0, so release2 will always higher than release1.
}
}

  空间复杂度o(1) 时间复杂度o(n)

理解一下各个字段 hold1 买入第一个商品的当前最大值 hold2买入第二个商品的当前最大值  release1卖出第一个商品的当前最大值  release2卖出第二个商品的当前最大值

不是很理解  决定在eclipse设置一个案例试试。

给定的数组为2,5,8,3,18,12,6,53,59,11

public static void main(String[] args){
test test =new test();
test.Solution solution=test.new Solution();
System.out.print(solution.maxProfit(new int[]{2,5,8,3,18,12,6,53,59,11}));
}

  输出结果为

看看调试中这四个变量的变化 在Hold1前设置断点

读入第一个数2 没什么大变化

读入第二个数5 release1和release2变成3

省略一些  读到18时看看各个值

release2就是目前阶段的解。。。   看不懂 先这样吧 - -

123. Best Time to Buy and Sell Stock (三) leetcode解题笔记的更多相关文章

  1. 188. Best Time to Buy and Sell Stock IV leetcode解题笔记

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

  2. 123. Best Time to Buy and Sell Stock III ——LeetCode

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

  3. LN : leetcode 123 Best Time to Buy and Sell Stock III

    lc 123 Best Time to Buy and Sell Stock III 123 Best Time to Buy and Sell Stock III Say you have an a ...

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

  5. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

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

  7. 【leetcode】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

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

  9. [LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 III

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

随机推荐

  1. 自定义UICollectinviewFlowLayout,即实现瀑布流

    如图所示,通过实现不规则的网格分布,来显示出不同的效果.因为集合视图必须要指定布局还可以显示,所以自定义布局就可以实现瀑布流的效果. //创建布局对象 WaterFlowLayout *flowLay ...

  2. cocos2d-js callFunc传参

    1.传递一个参数: pg.TestScene.prototype.init = function () { if (cc.Scene.prototype.init.call(this)) { var ...

  3. iOS 数据序列化,NSCoding, NSCoder

    iOS可以利用NSKeyedArchiver类将对象序列化成NSData存储在磁盘上,但前提是该对象所属的类必须遵从NSCoding协议. NSCoding协议包含两个方法,要序列化的类必须实现它们 ...

  4. 多层数据库应用基于Delphi DataSnap方法调用的实现(一)返回数据集

    从Delphi 2009开始,DataSnap技术发生了很大的变化,并在Delphi 2010和Delphi XE的后续版本中得到了持续的改进.Delphi 2009之前的DataSnap,虽然也实现 ...

  5. 学Android开发,入门语言java知识点

    学Android开发,入门语言java知识点 Android是一种以Linux为基础的开源码操作系统,主要使用于便携设备,而linux是用c语言和少量汇编语言写成的,如果你想研究Android,就去学 ...

  6. Android Studio doesn't list my android phone

    When USB debugger is openning, Android Studio doesn't list my phone, I cannot debug my app on my rea ...

  7. Java 如何跳出多重嵌套循环体?

    1. 在最外层循环语句前定义一个标号,循环体内任意位置都可以使用带有标号的break语句跳出外层循环,结束整个循环. System.out.println("MainClass1 Start ...

  8. json_decode返回null 和synax error原因及处理

    $checkLogin ='[{"gdsincode":"1103293","gdsname":"鲜美来带鱼段800g" ...

  9. Linux Svn 安装过程及配置

    重要的是第一步的安装,第二步配置可能没用,但是没试过,因为服务器上已经安装了第一步. 此处的第二步只为做个记录,说明一下里边的配置文件的用途. 3. 自己实际操作中的的配置记录(参照服务器别人的配置记 ...

  10. 如何将红色区域数据调用解密函数直接打印到输出控制台(例如:crt控制台)

    int main(int argc, char *argv[]) { unsigned char data[PACKET_MAX_LEN]; int data_len = 0; int socket_ ...