← 返回 microsoft 的题目列表Best Time to Buy and Sell Stock (single transaction) (write full main, include follow-ups)
类型:online_judge
Problem: Best Time to Buy and Sell Stock (single transaction) (must provide runnable main, include follow-ups)
Given an array prices where prices[i] is the stock price on day i, you may complete at most one transaction (buy once and sell once; must buy before sell). Return the maximum profit, or 0 if no profit is possible.
Input (stdin)
One line: integer array prices, e.g. [7,1,5,3,6,4]
Output (stdout)
One integer: the maximum profit
Constraints
1 <= n <= 2 * 10^5
0 <= prices[i] <= 10^9
Follow-ups
Unlimited transactions (cannot hold multiple shares at once).
Add a 1-day cooldown after selling.
Add a transaction fee fee per trade.
Example
Input
[7,1,5,3,6,4]
Output
5