Greedy Algorithms pattern

Best Time to Buy and Sell Stock

Given an array where prices[i] is the price of a stock on day i, maximize your profit by choosing one day to buy and a later day to sell. Return the maximum profit, or 0 if no profit is possible. Watch it run step by step, then write your own pseudocode and get it checked by AI.

  • LeetCode 121
  • Easy
  • 1 approach

Sign in with Google or GitHub and you land straight on this page. DSA comes with the all labs plan and every ChaiCode batch.

Greedy AlgorithmsLeetCode 121

Easy

The approach

  1. 01 Greedy · one pass tracking the minimum O(n)O(1) space
Played step by step, with its time and space.

What you will learn

  • How to read Best Time to Buy and Sell Stock and spot the greedy algorithms pattern in it
  • Greedy · one pass tracking the minimum: O(n) time, O(1) space
  • The pseudocode for each approach, its line lit as the animation runs
  • Practice: write your own pseudocode and get it checked

More Greedy Algorithms

See Best Time to Buy and Sell Stock run, step by step

Sign in and this page opens on the animation, with all 5 Greedy Algorithms problems beside it.

DSA comes with the all labs plan and every ChaiCode batch.