**一. 题目描述**
Say you have an array for which the i-th 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).
**二. 题目分析**
和前两道题相比,这道题限制了股票的交易次数,最多只能交易两次。
可使用动态规划来完成,首先是进行第一步扫描,先计算出序列`[0, …, i]`中的最大利润`profit`,用一个数组`f1`保存下来,这一步时间复杂度为`O(n)`。
第二步是逆向扫描,计算子序列`[i, …, n - 1]`中的最大利润`profit`,同样用一个数组`f2`保存下来,这一步的时间复杂度也是`O(n)`。
最后一步,对于,对`f1 + f2`,找出最大值即可。
**三. 示例代码**
~~~
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxProfit(vector<int> &prices)
{
int size = prices.size();
if (size <= 1) return 0;
vector<int> f1(size);
vector<int> f2(size);
int minV = prices[0];
for (int i = 1; i < size; ++i)
{
minV = std::min(minV, prices[i]);
f1[i] = std::max(f1[i - 1], prices[i] - minV);
}
int maxV = prices[size - 1];
f2[size - 1] = 0;
for (int i = size-2; i >= 0; --i)
{
maxV = std::max(maxV, prices[i]);
f2[i] = std::max(f2[i + 1], maxV - prices[i]);
}
int sum = 0;
for (int i = 0; i < size; ++i)
sum = std::max(sum, f1[i] + f2[i]);
return sum;
}
};
~~~
**四. 小结**
相比前两题,该题难度稍大,与该题相关的题目有好几道。后续更新…
- 前言
- 2Sum
- 3Sum
- 4Sum
- 3Sum Closest
- Remove Duplicates from Sorted Array
- Remove Duplicates from Sorted Array II
- Search in Rotated Sorted Array
- Remove Element
- Merge Sorted Array
- Add Binary
- Valid Palindrome
- Permutation Sequence
- Single Number
- Single Number II
- Gray Code(2016腾讯软件开发笔试题)
- Valid Sudoku
- Rotate Image
- Power of two
- Plus One
- Gas Station
- Set Matrix Zeroes
- Count and Say
- Climbing Stairs(斐波那契数列问题)
- Remove Nth Node From End of List
- Linked List Cycle
- Linked List Cycle 2
- Integer to Roman
- Roman to Integer
- Valid Parentheses
- Reorder List
- Path Sum
- Simplify Path
- Trapping Rain Water
- Path Sum II
- Factorial Trailing Zeroes
- Sudoku Solver
- Isomorphic Strings
- String to Integer (atoi)
- Largest Rectangle in Histogram
- Binary Tree Preorder Traversal
- Evaluate Reverse Polish Notation(逆波兰式的计算)
- Maximum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Longest Common Prefix
- Recover Binary Search Tree
- Binary Tree Level Order Traversal
- Binary Tree Level Order Traversal II
- Binary Tree Zigzag Level Order Traversal
- Sum Root to Leaf Numbers
- Anagrams
- Unique Paths
- Unique Paths II
- Triangle
- Maximum Subarray(最大子串和问题)
- House Robber
- House Robber II
- Happy Number
- Interlaving String
- Minimum Path Sum
- Edit Distance
- Best Time to Buy and Sell Stock
- Best Time to Buy and Sell Stock II
- Best Time to Buy and Sell Stock III
- Best Time to Buy and Sell Stock IV
- Decode Ways
- N-Queens
- N-Queens II
- Restore IP Addresses
- Combination Sum
- Combination Sum II
- Combination Sum III
- Construct Binary Tree from Inorder and Postorder Traversal
- Construct Binary Tree from Preorder and Inorder Traversal
- Longest Consecutive Sequence
- Word Search
- Word Search II
- Word Ladder
- Spiral Matrix
- Jump Game
- Jump Game II
- Longest Substring Without Repeating Characters
- First Missing Positive
- Sort Colors
- Search for a Range
- First Bad Version
- Search Insert Position
- Wildcard Matching