Best Time to Buy and Sell Stock Explained in 60 Seconds š | DSA #46
How do you maximize profit when you can buy once and sell once? You don't need to check every possible pair. š Track the lowest price seen so far š° For each day, calculate the profit if you sell today š Update the best profit when you find a better one ā¬ļø Then check if today's price is a new minimum š Repeat in one pass The critical order: Check today's profit first. Update the minimum second. Why? Your selling day must always come after the buying day. Example: Buy at the lowest price seen so far ā wait for a higher price ā calculate the profit ā keep the best result. Brute force: O(n²) One-pass approach: O(n) Extra space: O(1) The key idea: Track the lowest so far. Check profit before updating it. DSA Series #46 ā learning algorithms visually, one pattern at a time. #besttimetobuyandsellstock #stockprofit #dynamicprogramming #greedyalgorithm #dsa #datastructures #algorithms #coding #programming #computerscience #softwareengineering #leetcode #codinginterview #tech #developer #learncoding #100daysofdsa
Added
Know someone who'd love this clip?
Share it with friends and fellow fans.