A walkthrough of LeetCode problem 121 (Best Time to Buy and Sell Stock) covering three approaches: a brute force O(n²) nested loop solution that causes Time Limit Exceeded, an O(n) two-pointer approach in JavaScript, and an O(n) greedy approach in Python that tracks the minimum buy price seen so far.
Table of contents
Brute Force Approach (Time Complexity: O(n^2)) Copy link Link copied!Two Pointer Approach (Time Complexity: O(n)) Copy link Link copied!Greedy Approach (Time Complexity: O(n)) with Python Example Copy link Link copied!Sort: