← 返回 amazon 的题目列表Best Time to Buy & Sell Stock — Merged Variants
类型:qbank
Whiteboard progression through the stock-profit family: LC 121 (single transaction) and LC 122 (unlimited transactions) coded separately, then a follow-up merges both into one function, finishing with unlimited transactions plus a per-transaction fee (LC 714 family) with an optimization ask. Seen in an in-person L4 loop.
Requirements
Part 1: single-transaction maximum profit (buy once, sell once).
Part 2: unlimited-transaction maximum profit.
Follow-up A: merge both parts into a single function rather than two separate solutions.
Follow-up B: unlimited transactions with a per-transaction fee — continue optimizing.
Written on a physical whiteboard, no editor or execution.
Notes
Appears inside an in-person round that interleaves ~30 minutes of technical with ~30 minutes of LP questions — leave both whiteboard space and clock time for the fee variant.
The merge follow-up is the discriminating step: the ask is one parameterized routine, not two pasted solutions.
Preparation
Drill LC 121, 122, and 714 back-to-back, then practice collapsing them into one parameterized function.
Rehearse the hold/cash state formulation on a whiteboard — it absorbs the fee follow-up with a one-line change and survives the merge ask cleanly.