Compute Stock Profit with One or Unlimited Transactions
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Overview: Compute maximum stock profit for both one transaction and unlimited nonoverlapping transactions in linear time, returning zero when no positive trade is available.
Constraints
- 1 <= len(prices) <= 200000.
- Every price is a positive signed 32-bit integer.
- A sale must occur after its corresponding purchase, and at most one share may be held at a time.
- There are no fees, cooldown periods, or transaction limits for the unlimited objective.
- Return zero when no positive profit is possible, and use 64-bit arithmetic for total profit.
Examples
Input: ([7, 1, 5, 3, 6, 4],)
Expected Output: [5, 7]
Explanation: This is the source example: one transaction earns five, while two disjoint rises earn seven.
Input: ([5],)
Expected Output: [0, 0]
Explanation: One day permits no positive buy-sell transaction.
Hints
- Track the lowest price before each possible selling day for the one-transaction result.
- For unlimited transactions, capture every positive day-to-day increase.