← 返回 bytedance 的题目列表Best Time to Buy and Sell Stock (with follow-up for multiple transactions)
类型:online_judge
Given an integer array prices of length n, where prices[i] is the stock price on day i:
Compute the maximum profit with at most one buy and one sell.
Follow-up: If you may complete multiple transactions (you must sell before you buy again; you can hold at most one share at a time), compute the maximum profit.
Input Format
Line 1: integer n
Line 2: n integers representing prices
Output Format
Line 1: max profit with one transaction
Line 2: max profit with multiple transactions
Constraints
1 <= n <= 2 * 10^5
0 <= prices[i] <= 10^9
Examples
Example 1 input:
6
7 1 5 3 6 4
Output:
5
7
Example 2 input:
5
7 6 4 3 1
Output:
0
0
Example
Input
6
7 1 5 3 6 4
Output
5
7