leetcode — best-time-to-buy-and-sell-stock-iii
/**
* 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的更多相关文章
- 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 ...
- [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 ...
- [LeetCode] Best Time to Buy and Sell Stock III
将Best Time to Buy and Sell Stock的如下思路用到此题目 思路1:第i天买入,能赚到的最大利润是多少呢?就是i + 1 ~ n天中最大的股价减去第i天的. 思路2:第i天买 ...
- 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 ...
- [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 ...
- [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 ...
- 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 ...
- 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. ...
- 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 ...
- 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题使用动态 ...
随机推荐
- Django合集
Django基础 Django--简介 Django--web框架简介 浅析uWSGI.uwsgi.wsgi Django--url(路由)配置 Django--模板层 Django--视图层 Dja ...
- MVC5 Razor视图中不规范书写导致的编译问题
今天碰到一个非常让人难以理解的问题,如图所示,但是我在代码中并没有找到缺失"}"的地方: 根据源文件提示有去 AppData\Local\Temp\Temporary ASP.NE ...
- 一步一步 copy163: 网易严选 ---- vue-cli
面试点 组件间通信 生命周期函数 路由 - 参数 - 重定向 vuex 参考 网易严选商城小程序全栈开发,域名备案中近期上线(mpvue+koa2+mysql) 小程序服务端源码地址 小程序源码地址 ...
- 873D. Merge Sort
Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a w ...
- 反调试——jmp到那个地址
目录 1.前言 2.原理讲解 3.代码实现 前言 这节的反调试是通过构造代码来干扰正常的分析.反调试参考CrypMic勒索病毒 原理讲解 在逆向分析汇编代码时,一般都是通过汇编指令call或jmp跳到 ...
- swust oj 987
输出用先序遍历创建的二叉树是否为完全二叉树的判定结果 1000(ms) 10000(kb) 2553 / 5268 利用先序递归遍历算法创建二叉树并判断该二叉树是否为完全二叉树.完全二叉树只能是同深度 ...
- C语言复习6_doWhile循环
基本语法 do{ 循环操作 }while(循环条件); 特点:先执行,再判断 先执行一遍循环操作 符合条件,循环继续执行 否则循环退出 例题: #include <stdio.h> #in ...
- 【DFS】数独游戏
DFS(深度优先搜索): 深度优先搜索算法(英语:Depth-First-Search,简称DFS)是一种用于遍历或搜索树或图的算法. 沿着树的深度遍历树的节点,尽可能深的搜索树的分支.当节点v的所在 ...
- HTTP协议概念与特点,HTTP的状态码,HTTPS是什么?
很多人在打开网页的时候,在浏览器地址栏里都会看到http ,在Java WEB里,HTTP也是个重点内容,今天我们就来详细了解和学习HTTP . HTTP是Hyper Text Transfer P ...
- 推荐几个牛逼的 IDEA 插件,还带动图!
阅读本文大概需要 2.3 分钟. 作者:纪莫, cnblogs.com/jimoer 这里只是推荐一下好用的插件,具体的使用方法不一一详细介绍. JRebel for IntelliJ 一款热部署插件 ...