LeetCode – Refresh – Best Time to Buy and Sell Stock iii

III is a kind of I. But it require 2 maximum value. So scan from begin and scan from end to record the maximum value for currrent index.

Then scan the maximum array to obtain the global maximum.

 1 class Solution {
 2 public:
 3     int maxProfit(vector<int> &prices) {
 4         if (prices.size() < 2) return 0;
 5         int len = prices.size(), rec = prices[0], result = 0;
 6         vector<int> left(len, 0), right(len, 0);
 7         for (int i = 1; i < len; i++) {
 8             left[i] = max(left[i-1], prices[i] - rec);
 9             rec = min(rec, prices[i]);
10         }
11         rec = prices[len-1];
12         for (int i = len-2; i >= 0; i--) {
13             right[i] = max(right[i+1], rec - prices[i]);
14             rec = max(prices[i], rec);
15         }
16         for (int i = 0; i < len; i++) {
17             result = max(result, left[i] + right[i]);
18         }
19         return result;
20     }
21 };
时间: 2024-10-15 04:18:40

LeetCode – Refresh – Best Time to Buy and Sell Stock iii的相关文章

[LeetCode] 123. Best Time to Buy and Sell Stock III 买卖股票的最佳时间 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,

【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】Best Time to Buy and Sell Stock III (2 solutions)

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][Java] 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

LeetCode – Refresh – Best Time to Buy and Sell Stock ii

This is simple, use greedy algorithm will solve the problem. 1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 int len = prices.size(), result = 0; 5 if (len < 2) return 0; 6 for (int i = 0; i < len-1; i++) { 7 if (pric

LeetCode – Refresh – Best Time to Buy and Sell Stock iv

This is combination of II and III. 1. when k >= length, it is totally II. 2. when k < length, it is another III. A localMaximum and globalMaximum subarray need to be maintained. But we can reduce the two dimensional DP to one dimensional DP localMax

LeetCode – Refresh – Best Time to Buy and Sell Stock

1 class Solution { 2 public: 3 int maxProfit(vector<int> &prices) { 4 if (prices.size() < 2) return 0; 5 int len = prices.size(), result = 0, rec = prices[0]; 6 for (int i = 1; i < len; i++) { 7 result = max(result, prices[i] - rec); 8 rec

leetcode 【 Best Time to Buy and Sell Stock III 】python 实现

题目: 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 123. Best Time to Buy and Sell Stock III JAVA语言

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,