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. Linux Top 命令解析 比较详细

    [尊重原创文章出自:http://www.jb51.net/LINUXjishu/34604.html] TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占 ...

  2. iOS中如何切换到发短信、打电话、发邮件

    我们在做APP的时候,难免会遇到需要调用短信,电话等程序的时候.如美团. 当然,这些都只是一些简单的方法就可以实现,但是时间久了也会淡忘,所以想写这边博客.一是为了再捡起来复习一下,另一个相当于留个备 ...

  3. CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files 解决方案

    1 设置c:windows\temp 目录访问权限 temp--> 属性-->安全-- > 添加network service -->并赋予其权限为 读 和 写--> 确 ...

  4. sql2008r2-vs2013安装下载

    vs2013链接:http://www.cnblogs.com/wuyepiaoxue/p/5661174.html sql2008r2链接:http://download.microsoft.com ...

  5. EUI List列表实现人物列表

    一  Scroll+List ,拖动组件到exml. List不能写定高度,不然无法自动扩展.  二 新建List条目皮肤, ListItemSkin皮肤 名字Label的文本{data.name} ...

  6. poi批量导入excel文件

    package com.practice.util; import java.io.File; import java.io.FileInputStream; import java.io.FileN ...

  7. ASP.NET Misconfiguration: Request Validation Disabled

    Abstract: Use the ASP.NET validation framework to prevent vulnerabilities that result from unchecked ...

  8. LinQ的组合+分页

    前台代码: 名称:<asp:TextBox ID="Textname" runat="server"></asp:TextBox> 油耗 ...

  9. Android Studio中清单文件改versionCode和versionName没效果的原因

    在Android Studio中,项目的versionCode 和versionName 的控制不是在AndroidManifest.xml清单文件中更改的,而是在项目的build.gradle中更改 ...

  10. 前端资源构建-Grunt环境搭建

    前端资源构建-Grunt 随着前端开发的复杂度越来越高,前端页面动辄上几十个js,十几个html页面.用户打开一个页面需要加载一堆的css,js,html文件,对于有大量用户的web应用来说,既消耗服 ...