Maximize Trading Profit with a One-Day Cooldown

Quick Overview

Practice maximizing repeated trading profit with a one-day wait after each sale. Track purchase eligibility, preserve single-share ownership, and handle zero prices and unprofitable inputs.

Maximize Trading Profit with a One-Day Cooldown

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

# Maximize Trading Profit with a One-Day Cooldown Given daily prices `prices`, return the maximum profit obtainable by buying and selling one share at a time. You may make multiple transactions, but you must sell a held share before buying another. After selling on a day, you cannot buy on the immediately following day. This is a one-day cooldown. You may choose to make no transaction. ### Function `int maxProfit(vector<int>& prices)` ### Example 1 ```text Input: prices = [2,6,1,2,7] Output: 9 ``` Buy at `2` on day `0` and sell at `6` on day `1`, earning `4`. Day `2` is the cooldown. Buy at `2` on day `3` and sell at `7` on day `4`, earning `5` more. ### Example 2 ```text Input: prices = [9,6,3] Output: 0 ``` No profitable completed transaction is available. ### Constraints - `1 <= prices.length <= 5000`. - `0 <= prices[i] <= 1000`. ### Clarifications Profit is cash earned from completed sales minus purchase costs. Return the maximum profit value, not the sequence of trades. A price of zero is valid. An unfinished purchase is not itself a completed profit. ### What a Strong Answer Covers - How sale history affects eligibility to buy on a later day. - Why simultaneous ownership of multiple shares is excluded. - A single day, declining prices, and a tempting purchase during cooldown. - A correctness argument and time/storage bounds.

Overview: Practice maximizing repeated trading profit with a one-day wait after each sale. Track purchase eligibility, preserve single-share ownership, and handle zero prices and unprofitable inputs.

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Maximize Trading Profit with a One-Day Cooldown

Given daily prices prices, return the maximum profit obtainable by buying and selling one share at a time. You may make multiple transactions, but you must sell a held share before buying another.

After selling on a day, you cannot buy on the immediately following day. This is a one-day cooldown. You may choose to make no transaction.

Function

int maxProfit(vector<int>& prices)

Example 1

Input: prices = [2,6,1,2,7]
Output: 9

Buy at 2 on day 0 and sell at 6 on day 1, earning 4. Day 2 is the cooldown. Buy at 2 on day 3 and sell at 7 on day 4, earning 5 more.

Example 2

Input: prices = [9,6,3]
Output: 0

No profitable completed transaction is available.

Constraints

  • 1 <= prices.length <= 5000 .
  • 0 <= prices[i] <= 1000 .

Clarifications

Profit is cash earned from completed sales minus purchase costs. Return the maximum profit value, not the sequence of trades. A price of zero is valid. An unfinished purchase is not itself a completed profit.

What a Strong Answer Covers Guidance

  • How sale history affects eligibility to buy on a later day.
  • Why simultaneous ownership of multiple shares is excluded.
  • A single day, declining prices, and a tempting purchase during cooldown.
  • A correctness argument and time/storage bounds.