Skip to main content
MarketVault
BrowseExpertsTopicsTimelineMapSubmit

Disclaimer: MarketVault is an educational video curation platform. Nothing on this site constitutes financial advice, investment advice, or a recommendation to buy or sell any asset. Always consult a qualified, regulated financial advisor before making investment decisions. Investing carries risk — you may lose money.

MarketVault

Curated financial insights from the world's top experts. Invest in your knowledge.

BrowseExpertsTopicsDecadesSubmit a ClipAboutContactEditorial PolicyArticles

© 2026 MarketVault. All footage remains the property of its original creators.

Privacy PolicyTerms of UseSupport

Developed with love as a personal project by Jamie McDonnell

ui-ux-design.comai-consultancy.company
LeetCode 121 Best Time to Buy and Sell Stock (Python) | One Pass 📈 | #programming — MarketVault
PreviousUse arrow keysNext
0 views
Share this clip

LeetCode 121 Best Time to Buy and Sell Stock (Python) | One Pass 📈 | #programming

Expert Interviewyoutube

LeetCode 121 Best Time to Buy and Sell Stock, explained in one pass with Python 🐍 Remember the cheapest price so far and ask every day: "what if I sold today?" O(n) time, O(1) space. 📌 Example: prices = [7, 1, 5, 3, 6, 4] → buy at 1, sell at 6, profit 5. ❌ The trap: "buy at the lowest, sell at the highest" fails. Here the highest price (7) comes BEFORE the lowest (1). You can't sell before you buy. 🐌 Two loops (every buy day × every sell day) works, but 100,000 days = about five billion pairs: Time Limit Exceeded. ✅ The trick: walk the days once. Keep the cheapest price so far, and the best profit if you sold today. 💡 3 tips: 1. Start best at 0. If prices only fall ([7, 6, 4, 3, 1]), you never trade and 0 is the right answer. 2. Pattern: when today's answer needs the best of the past, carry a running minimum. 3. Interview flex: it's Kadane's algorithm on the daily price changes (-6, +4, -2, +3, -2 → best run = 5). 🎯 Play along: [3, 8, 1, 5], one trade. The cheapest day (1) is NOT the buy day. Answer: 5 (buy 3, sell 8). 💻 Code (Python): def maxProfit(prices): cheapest = prices[0] best = 0 for p in prices: cheapest = min(cheapest, p) best = max(best, p - cheapest) return best 📚 Problem 7 of LeetCode's Top Interview 150, in order. Next: Best Time to Buy and Sell Stock II (unlimited trades). Logos: Simple Icons (CC0); trademarks belong to their owners. Animated emoji: Noto Emoji Animation by Google, CC BY 4.0. #leetcode #python #shorts #besttimetobuy #buyandsell #codinginterview #LeetCode121 #TopInterview150 #dsa #algorithms #learntocode #interviewpreparation #programming



Know someone who'd love this clip?

Share it with friends and fellow fans.

Share this clip

Keep Exploring

All ExpertsAll TopicsAll DecadesBrowse by Format

Added 17 Oct 2026

More Expert Interview Clips

View all →
Thumbnail for Before You Sell: 5 Investing Lessons From Warren Buffett4:08

Before You Sell: 5 Investing Lessons From Warren Buffett

2020sExpert Interview
Thumbnail for ETFs or Direct shares? Two ways to invest in the stock market. #rivkinreport #rivkin #etfs #shares0:38

ETFs or Direct shares? Two ways to invest in the stock market. #rivkinreport #rivkin #etfs #shares

2020sExpert Interview
Thumbnail for LeetCode 122 Best Time to Buy and Sell Stock II | C++ Solution | Greedy Approach | DSA0:50

LeetCode 122 Best Time to Buy and Sell Stock II | C++ Solution | Greedy Approach | DSA

Expert Interview
Thumbnail for Why You’re Scared To Quit (One More Year Syndrome)1:12

Why You’re Scared To Quit (One More Year Syndrome)

Expert Interview