Best Time to Buy and Sell Stock III 解答

Question

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).

Solution

This problem can be solved by "divide and conquer". We can use left[i] array to track maximum profit for transactions before i (including i), and right[i + 1] to track maximum profit for transcations after i.

Prices: 1 4 5 7 6 3 2 9
left = [0, 3, 4, 6, 6, 6, 6, 8]
right= [8, 7, 7, 7, 7, 7, 7, 0]

Time complexity O(n), space cost O(n).

 1 public class Solution {
 2     public int maxProfit(int[] prices) {
 3         if (prices == null || prices.length < 2)
 4             return 0;
 5         int length = prices.length, min = prices[0], max = prices[length - 1], tmpProfit = 0;
 6         int[] leftProfits = new int[length];
 7         leftProfits[0] = 0;
 8         int[] rightProfits = new int[length];
 9         rightProfits[length - 1] = 0;
10         // Calculat left side profits
11         for (int i = 1; i < length; i++) {
12             if (prices[i] > min)
13                 tmpProfit = Math.max(tmpProfit, prices[i] - min);
14             else
15                 min = prices[i];
16             leftProfits[i] = tmpProfit;
17         }
18         // Calculate right side profits
19         tmpProfit = 0;
20         for (int j = length - 2; j >= 0; j--) {
21             if (prices[j] < max)
22                 tmpProfit = Math.max(tmpProfit, max - prices[j]);
23             else
24                 max = prices[j];
25             rightProfits[j] = tmpProfit;
26         }
27         // Sum up
28         int result = Integer.MIN_VALUE;
29         for (int i = 0; i < length - 1; i++)
30             result = Math.max(result, leftProfits[i] + rightProfits[i + 1]);
31         result = Math.max(result, leftProfits[length - 1]);
32         return result;
33     }
34 }
时间: 2024-11-04 14:46:46

Best Time to Buy and Sell Stock III 解答的相关文章

Best Time to Buy and Sell Stock III

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. c++版本代码: class Solution { public: i

leetcode——Best Time to Buy and Sell Stock III 买卖股票最大收益(AC)

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,

【leetcode】Best Time to Buy and Sell Stock III

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

LeetCode 笔记23 Best Time to Buy and Sell Stock III

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. 现在A股涨这么好,要是股票都提前知道价格就好了(@[email pro

123. Best Time to Buy and Sell Stock III

/* * 123. Best Time to Buy and Sell Stock III * 2016-5-21 by Mingyang * 根据题目要求,最多进行两次买卖股票,而且手中不能有2只股票,就是不能连续两次买入操作. * 所以,两次交易必须是分布在2各区间内,也就是动作为:买入卖出,买入卖出. * 进而,我们可以划分为2个区间[0,i]和[i,len-1],i可以取0~len-1. * 那么两次买卖的最大利润为:在两个区间的最大利益和的最大利润. * 一次划分的最大利益为:Prof

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 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

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 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 en

【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 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,

LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

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,