/**
* 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. springboot添加邮件发送及压缩功能

    springboot添加邮件发送及文件压缩功能 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190233.html 先来一段诗 ``` 就这样吧 忍受折磨 ...

  2. JS精度问题(0.1+0.2 = 0.3吗?)

    一.引出问题 0.1+0.2 = 0.3吗?在JS中是这样的吗?我们写个测试代码不就知道了吗? 结果出人意料,并不像我们所想象的那样.那么这到底是为什么呢? 二.原因分析 JS浮点数存储机制: 三.解 ...

  3. wamp apache 设置多端口

  4. OI暑假集训游记

    莞中OI集训游记 Written BY Jum Leon. I        又是一载夏,本蒟蒻以特长生考入莞中,怀着忐忑的心情到了8月,是集训之际.怀着对算法学习的向往心情被大佬暴虐的一丝恐惧来到了 ...

  5. 微信跳转ticket值怎么得到?浏览器跳到微信?哪里有微信跳转接口?跳转功能能用多久?

    目前很多实用微信跳转技术的电商朋友,网站文章头部或者文章中部出现了点击关注微信关注的二维码,用户点击进去直接跳转到微信内打开指定的二维码,识别即可关注,方便省事,比以往的一键复制—粘贴微信号,转化效果 ...

  6. TS+React+Redux 使用之搭建环境

    使用 create-react-app 构建 1.全局安装create-react-app npm install -g create-react-app 2.创建一个项目 create-react- ...

  7. [复试机试]c++读取/写入文本文件

    读取文件 #include <iostream> #include <cstdio> #include <string> #include <cstdlib& ...

  8. 在线生成透明ICO图标神器

    此神器的链接为:http://ico.duduxuexi.com/ 大家可以将这个网址收藏好,本人亲测十分好用!对我们的ios,安卓以及windows开发都有极大的好处.

  9. Hadoop 数据去重

    数据去重这个实例主要是为了读者掌握并利用并行化思想对数据进行有意义的筛选.统计大数据集上的数据种类个数.从网站日志中计算访问等这些看似庞杂的任务都会涉及数据去重.下面就进入这个实例的MapReduce ...

  10. Android 音视频开发(五):使用 MediaExtractor 和 MediaMuxer API 解析和封装 mp4 文件

    一个音视频文件是由音频和视频组成的,我们可以通过MediaExtractor.MediaMuxer把音频或视频给单独抽取出来,抽取出来的音频和视频能单独播放: 一.MediaExtractor API ...