Why does trying every buy/sell pair become slow?

Practice this question from Best Time to Buy and Sell Stock - Greedy Pattern Practice Quiz, with options and an explanation.

Question

Why does trying every buy/sell pair become slow?

Option A

It sorts the prices incorrectly

Option B — Correct answer

It checks too many buy/sell pairs

Option C

It cannot return zero

Option D

It uses a stack

Explanation

Each buy day may be paired with many later sell days, causing O(n^2) work.

Best Time to Buy and Sell Stock - Greedy Pattern Practice Quiz

All questions · Previous question · Next question

Open on QuizMaker