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题使用动态 ...
随机推荐
- 使用handler倒计时
package com.example.jikangwang.myapplication; import android.content.Intent; import android.os.Handl ...
- Java 什么是线程安全
当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些线程将如何交替执行,并且在主调代码中不需要额外的同步或协同,这个类都能表现出正确的行为,那么这个类就是线程安全的.其中,正确性指某个类的行 ...
- Linux进阶命令用法
1.tr命令 可以对来自标准输入的字符进行替换.压缩和删除.它可以将一组字符变成另一组字符 选项 -c或——complerment:取代所有不属于第一字符集的字符: -d或——delete:删除所有属 ...
- ios开发中的深拷贝和浅拷贝
这是一个老生常谈的话题,面试中也经常被问到,下面总结一下自己的一些心得. 一句话总结: 浅拷贝就是指针拷贝: 深拷贝是对象本身的拷贝: 下面一张抽象的图可以直观的表述出两句话的内涵 其实这里还引申出了 ...
- PTA第三次作业
---恢复内容开始--- 题目 7-1 计算职工工资 1.设计思路 (1)第一步:观察题意了解各个参数与所需函数在题目中的意义: 第二步:设计算法编写函数,让函数的功能实现题目中所需的功能: 第三步: ...
- [Swift]LeetCode11. 盛最多水的容器 | Container With Most Water
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). ...
- [Swift]LeetCode273. 整数转换英文表示 | Integer to English Words
Convert a non-negative integer to its english words representation. Given input is guaranteed to be ...
- [Swift]LeetCode387. 字符串中的第一个唯一字符 | First Unique Character in a String
Given a string, find the first non-repeating character in it and return it's index. If it doesn't ex ...
- [Swift]LeetCode640. 求解方程 | Solve the Equation
Solve a given equation and return the value of x in the form of string "x=#value". The equ ...
- Java中的String,StringBuilder,StringBuffer三者的区别(转载)
最近在学习Java的时候,遇到了这样一个问题,就是String,StringBuilder以及StringBuffer这三个类之间有什么区别呢,自己从网上搜索了一些资料,有所了解了之后在这里整理一下, ...