← 返回 amazon 的题目列表Stock Trading Profit Calculator
类型:online_judge
Problem: Stock Trading Profit Calculator
You are given an integer array prices, where prices[i] is the stock price on day i.
Implement a function that supports the following two trading modes:
max_transactions = 1: You may complete at most one transaction, meaning one buy and one sell.
max_transactions = -1: You may complete unlimited transactions, but you may hold at most one share at any time. A transaction fee fee is charged every time you sell.
Return the maximum profit achievable under the given rules.
A transaction consists of buying once and selling once. You cannot sell before buying.
Input Format
n
prices[0] prices[1] ... prices[n-1]
max_transactions fee
n: length of the price array
prices: daily stock prices
max_transactions: trading mode, either 1 or -1
fee: transaction fee charged on each sell; ignored when max_transactions = 1
Output Format
maximum profit
Constraints
1 <= n <= 100000
0 <= prices[i] <= 100000
max_transactions in {1, -1}
0 <= fee <= 100000
Example
Input:
6
7 1 5 3 6 4
1 0
Output:
5
Explanation: Buy at price 1 and sell at price 6, for a profit of 5.
Example
Input
6
7 1 5 3 6 4
1 0
Output
5