← 返回 meta 的题目列表Best Time to Buy and Sell Stock
类型:qbank
LC 121 — maximum profit from a single buy / sell of a stock price array. Asked in a recent E5 onsite coding round, with a follow-up extending to multiple transactions.
Requirements
Input: integer array prices where prices[i] is day i's price.
Return the maximum profit from at most one buy followed by one sell. If no profit is possible, return 0.
Follow-up reported on-loop: extend to unlimited transactions (LC 122) or at most k transactions (LC 188).
Notes
Single-pass: track min_price so far and the best profit seen against that minimum.
For the unlimited-transactions follow-up: sum every positive prices[i] - prices[i-1]; the greedy proof is short and worth practicing aloud.
For the k-transaction follow-up: 2D DP indexed by day and remaining transactions, then the O(k·n) rolling-array trick.
Preparation
Write the one-pass solution in under 5 minutes; rehearse the follow-up extension verbally so the interviewer doesn't have to pull it out of you.