题目链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/description/

题目大意:与122题类似,只是这里要求买卖次数只能有两次,计算两次总共的最大利润值。

法一(超时):计算某一天之前的最大利润值与某一天之后的最大利润值,然后用for循环遍历得到最大值,时间复杂度是o(n^2),代码如下:

 int res = 0;
for(int i = 0; i < prices.length; i++) {
int maxPre = 0;
int min = Integer.MAX_VALUE;
//0 to i-1
for(int j = 0; j < i; j++) {
if(prices[j] < min) {
min = prices[j];
}
else if(prices[j] - min > maxPre){
maxPre = prices[j] - min;
}
}
//i to n-1
int maxPost = 0;
min = Integer.MAX_VALUE;
for(int j = i; j < prices.length; j++) {
if(prices[j] < min) {
min = prices[j];
}
else if(prices[j] - min > maxPost){
maxPost = prices[j] - min;
}
} if(maxPre + maxPost > res) {
res = maxPre + maxPost;
}
}
return res;

法二(借鉴):动态规划,利用法一的思路,只是这里不用for循环嵌套遍历得到最大值,而是用两个数组分别记录某一天之前的最大利润值和某一天之后的最大利润值,然后再另开一个for循环,计算比较得到最大值。这里有两个动规公式:

1.数组记录最大利润:

  1)某一天之前:preProfit[i] = max(preProfit[i - 1], prices[i] - min);

  2)某一天之后:postProfit[i] = max(postProfit[i + 1], max - prices[i]);

2.for循环计算得到最大值:res = max(res, preProfit[i] + postProfit[i]);

代码如下(耗时1ms):

         int length = prices.length;
int[] preProfit = new int[length];
int[] postProfit = new int[length];
//从前往后找,0 to i,用数组记录每一天i之前所能获得的最大利润,计算过程与121题类似
int minPrice = Integer.MAX_VALUE;
int max = 0;
for(int i = 0; i < length; i++) {
if(prices[i] < minPrice) {
minPrice = prices[i];
}
else if(prices[i] - minPrice > max) {
max = prices[i] - minPrice;
}
preProfit[i] = max;
}
//从后往前,i to n-1,用数组记录每一天i之后所能获得的最大利润
//注意:从后往前找的时候,应该记录当前位置之后的最大价值,然后将当前位置的价值与最大价值进行比较
int maxPrice = Integer.MIN_VALUE;
max = 0;
for(int i = length - 1; i >= 0; i--) {
if(prices[i] > maxPrice) {
maxPrice = prices[i];
}
else if(maxPrice - prices[i] > max) {
max = maxPrice - prices[i];
}
postProfit[i] = max;
} int res = 0;
for(int i = 0; i < length; i++) {
if(preProfit[i] + postProfit[i] > res) {
res = preProfit[i] + postProfit[i];
}
}
return res;

法三(借鉴):参考:http://blog.csdn.net/u012501459/article/details/46514309解法二。

123.Best Time to Buy and Sell Stock III---dp的更多相关文章

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

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

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

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

  5. 【刷题-LeetCode】123 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

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

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

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

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

  10. 123. Best Time to Buy and Sell Stock III (Array; DP)

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

随机推荐

  1. iOS----MRC(手动内存管理)

    1.MRC是什么,有什么用? 在苹果开发中,我们是没有垃圾回收机制的.所以在ARC推出之前,我们苹果开发程序员需要通过手动代码的形式尽量严密的管理我们的App的内存: ---------------- ...

  2. 程序猿必备技能:数据库管理——关于MySQL

    一.初识MySQL 1.什么是数据库? 数据库(Database,DB)简而言之就是存放数据的仓库,是为了实现一定目的,按照某种规则组织起来的数据的集合. 2.使用数据库的必要性 (1)结构化存储大量 ...

  3. GLSL反转矩阵inverse

    低版本 vertex shader 可以使用,通常用来反转TBN矩阵,但是计算量很大. 代码来自 OpenGL Mathematics (GLM) mat4 inverse_mat4(mat4 m) ...

  4. CF530D sum in the tree

    我是题面.原题地址 很简单的一道贪心题 首先,先想想怎么判断是否合法 题目中说,a是自然数,那么子节点的s明显是不能比父节点大的,如果比父节点大,不合法! 所有深度为偶数的点的s被删除了,也只有深度为 ...

  5. Java进程配置文件Reload

    我们在开发Java程序的时候,很多常量信息都存在配置文件中,比如数据库连接信息.ip黑名单,事件的超时时间等等.当需要该这些配置的值时都需要重新启动进程,改动的配置才会生效,有时候线上的应用不能容忍这 ...

  6. 【BZOJ4516】生成魔咒(后缀自动机)

    [BZOJ4516]生成魔咒(后缀自动机) 题面 BZOJ Description 魔咒串由许多魔咒字符组成,魔咒字符可以用数字表示.例如可以将魔咒字符 1.2 拼凑起来形成一个魔咒串 [1,2]. ...

  7. 洛谷P3933 Chtholly Nota Seniorious 【二分 + 贪心 + 矩阵旋转】

    威廉需要调整圣剑的状态,因此他将瑟尼欧尼斯拆分护符,组成了一个nnn行mmm列的矩阵. 每一个护符都有自己的魔力值.现在为了测试圣剑,你需要将这些护符分成 A,B两部分. 要求如下: 圣剑的所有护符, ...

  8. Codeforces 480.E Parking Lot

    E. Parking Lot time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  9. C++并发编程 互斥和同步

    C++并发编程 异步任务(async) 线程基本的互斥和同步工具类, 主要包括: std::mutex 类 std::recursive_mutex 类 std::timed_mutex 类 std: ...

  10. isNaN与parseInt/parseFloat

    isNaN 返回一个 Boolean 值,指明提供的值是否是保留值 NaN (不是数字). NaN 即 Not a Number isNaN(numValue) 必选项 numvalue 参数为要检查 ...