/**
* Source : https://oj.leetcode.com/problems/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).
*/
public class BestTimeToBuyAndSellStock3 { /**
* 求出最大利润,本题目限制了买卖次数为2,同一时刻只能持有一只股票
* 因为不能同时持有一支以上股票,所以两次买卖的时间不会重叠,所以可以把prices分割为两段,求出每段的最大值,然后将两个最大值相加得到最大利润
* 但是不确定怎么分割prices,所以要尝试每一种分割方法,以i为分割点pricesp[0:n] = prices[0:i] + prices[i:n]
* 求出每种分割情况下两段的最大值,针对每一种分割后的情况使用stcok1中的解法
*
* 这里先使用循环实现
*
* @param prices
* @return
*/
public int maxProfit (int[] prices) {
if (prices.length <=1 ) {
return 0;
}
int maxProfit = Integer.MIN_VALUE;
for (int i = 1; i < prices.length-1; i++) {
int min = prices[0];
int max = Integer.MIN_VALUE;
for (int j = 0; j <= i; j++) {
if (min > prices[j]) {
min = prices[j];
} else {
int profit = prices[j] - min;
max = profit > max ? profit : max;
}
} int max2 = Integer.MIN_VALUE;
min = prices[i];
for (int j = i; j < prices.length; j++) {
if (min > prices[j]) {
min = prices[j];
} else {
int profit = prices[j] - min;
max2 = profit > max2 ? profit : max2;
}
}
max = max2 + max;
maxProfit = max > maxProfit ? max : maxProfit; }
return maxProfit;
} /**
* 使用DP来优化时间复杂度,上面时间复度是O(n^2)
* 正向循环先计算出分割后的第一个部分的最大值,i从1:n-2分割后的第一部分的最大值存储在数组中
* 然后逆向循环计算分割后第二部分的最大值,再与该位置第一部分计算得出的最大值相加就是该种分割情况下的最大值
* 循环完成后扫描结果数组中的最大值就是所要求的最大利润
*
* @param prices
* @return
*/
public int maxProfitByDP (int[] prices) {
if (prices.length <= 1) {
return 0;
}
int[] maxArr = new int[prices.length];
int max = 0;
int min = prices[0];
for (int i = 0; i < prices.length; i++) {
if (prices[i] < min) {
min = prices[i];
} else {
int profit = prices[i] - min;
max = profit < max ? max : profit;
}
maxArr[i] = max;
} int result = Integer.MIN_VALUE;
max = 0;
int maxRightProfit = 0;
for (int i = prices.length-1; i > 0; i--) {
if (prices[i] > max) {
max = prices[i];
} else {
int profit = max - prices[i];
maxRightProfit = profit > maxRightProfit ? profit : maxRightProfit;
}
result = Math.max(result, maxRightProfit + maxArr[i]);
}
return result;
} public static void main(String[] args) {
BestTimeToBuyAndSellStock3 bestTimeToBuyAndSellStock3 = new BestTimeToBuyAndSellStock3();
System.out.println(bestTimeToBuyAndSellStock3.maxProfit(new int[]{1,2,3,5,-1,4,7}));
System.out.println(bestTimeToBuyAndSellStock3.maxProfitByDP(new int[]{1,2,3,5,-1,4,7}));
}
}

leetcode — best-time-to-buy-and-sell-stock-iii的更多相关文章

  1. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

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

  3. [LeetCode] Best Time to Buy and Sell Stock III

    将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...

  4. LeetCode: Best Time to Buy and Sell Stock III [123]

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

  5. [Leetcode] Best time to buy and sell stock iii 买卖股票的最佳时机

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

  6. [leetcode]Best Time to Buy and Sell Stock III @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 题意: Say you have an array ...

  7. leetcode -- Best Time to Buy and Sell Stock III TODO

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

  8. LeetCode——Best Time to Buy and Sell Stock III

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

  9. LeetCode——Best Time to Buy and Sell Stock III (股票买卖时机问题3)

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

  10. LeetCode OJ--Best Time to Buy and Sell Stock III

    http://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/ 这三道题,很好的进阶.1题简单处理,2题使用贪心,3题使用动态 ...

随机推荐

  1. 通过源码理解HashMap的并发问题

    最近在学习有关于Java的基础知识,在学习到HashMap的相关知识的时候,了解了HashMap的并发中会出现的问题,在此记录,加深理解(这篇文章是基于Java1.7的,主要是为了更加直观,更新版本的 ...

  2. Python(Django)遇到的问题及解决方法

    问题一 因为已经有程序占用了Django的默认端口了,所以只要这么启动项目,81是使用的端口,然后访问即可http://127.0.0.1:81/ 解决: 问题二 TypeError: not eno ...

  3. javascript 插入DOM节点

    1.使用appendChild,把一个子节点添加到父节点的最后一个子节点,.innerText插入的是内容 HTML <!-- HTML结构 --> <p id="js&q ...

  4. Python 版本管理anaconda

    下载安装 下载地址 :anaconda官网 下载后直接命令行安装,默认安装按enter 和yes bash Anaconda3-5.2.0-Linux-x86_64.sh 按照官网上下一步直接用con ...

  5. c++ 之bind使用

    网络编程中, 经常要使用到回调函数. 当底层的网络框架有数据过来时,往往通过回调函数来通知业务层. 这样可以使网络层只专注于 数据的收发, 而不必关心业务 在c语言中, 回调函数的实现往往通过函数指针 ...

  6. C++或C#调用外部exe的分析

    假如有个外部程序名为A.exe,放在目录E:\temp\下,然后我们用C++或者C#写一个程序调用这个A.exe的话(假设这个调用者所在的路径在D:\invoke),通常会采用下面的代码: // C# ...

  7. [Swift]LeetCode529. 扫雷游戏 | Minesweeper

    Let's play the minesweeper game (Wikipedia, online game)! You are given a 2D char matrix representin ...

  8. [Swift]LeetCode655. 输出二叉树 | Print Binary Tree

    Print a binary tree in an m*n 2D string array following these rules: The row number m should be equa ...

  9. [Swift]LeetCode706. 设计哈希映射 | Design HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  10. 什么是REST接口

    转载自:http://baijiahao.baidu.com/s?id=1591007540303121112&wfr=spider&for=pc 从事web开发工作有一小段时间,RE ...