/**
* 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. BZOJ.5397.circular(随机化 贪心)

    BZOJ 感觉自己完全没做过环上选线段的问题(除了一个2-SAT),所以来具体写一写qwq. 基本完全抄自remoon的题解qwq... (下标从\(0\sim m-1\)) 拆环为链,对于原线段\( ...

  2. arguments伪对象数组 javascript

    arguments伪对象数组: <!DOCTYPE html> <html lang="en"> <head> <meta charset ...

  3. Linux下如何查看定位当前正在运行的Nginx的配置文件

    1. 查看nginx的PID,以常用的80端口为例: [root@xiaoyuer scripts]# netstat -lntup|grep 80 tcp 0 0 0.0.0.0:80 0.0.0. ...

  4. Paper Reading——LEMNA:Explaining Deep Learning based Security Applications

    Motivation: The lack of transparency of the deep  learning models creates key barriers to establishi ...

  5. android颜色color.xml设置

     XML Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 ...

  6. High Availability手册(2): 架构

    最底层是通信层corosync/openais 负责cluster中node之间的通信 上一层是Resource Allocation Layer,包含下面的组件: CRM Cluster Resou ...

  7. 在 Ubuntu 中使用 Visual Studio Code

    前言 我一直在 Linux 桌面系统下的探索寻找各种界面美观.使用舒适的软件工具.对于Linux下的开发人员来讲,这几年最大的福利就是 MicroSoft 推出的 Visual Studio Code ...

  8. jsplumb 中文教程

    https://wdd.js.org/jsplumb-chinese-tutorial/#/ 1. jsplumb 中文基础教程 后续更新会在仓库:https://github.com/wangdua ...

  9. 【RL-TCPnet网络教程】第34章 RL-TCPnet之SMTP客户端

    第34章      RL-TCPnet之SMTP客户端 本章节为大家讲解RL-TCPnet的SMTP应用,学习本章节前,务必要优先学习第33章的SMTP基础知识.有了这些基础知识之后,再搞本章节会有事 ...

  10. Redis缓存实现排序功能

    如果对实时并发排序感兴趣,请关注这个项目(java):https://github.com/xuerong/hqrank,欢迎参与开发,pass:支持多字段排行 最近遇到一个问题就是根据需求需要对所有 ...