Перейти к содержимому

Best Time to Buy and Sell Stock | LeetCode 121 | Two Pass Approach | C++

Hack Code

0:00 / 0:00

Best Time to Buy and Sell Stock | LeetCode 121 | Two Pass Approach | C++

4 просмотра · 11 дн. назад
Hack Code
11 подписчиков
4 просмотра · 11 дн. назад
In this video, I solve LeetCode 121 – Best Time to Buy and Sell Stock using C++ with a simple two-pass approach. I explain how to use two separate for loops to solve the problem step by step. The first loop finds the minimum stock price, and the second loop calculates the maximum profit. 🔹 Problem: Best Time to Buy and Sell Stock 🔹 LeetCode: #121 🔹 Difficulty: Easy 🔹 Topic: Arrays 🔹 Language: C++ 🔹 Time Complexity: O(n) + O(n) = O(2n) = O(n) Note: Although we use two separate loops, the overall time complexity simplifies to O(n) because constant factors are ignored in Big-O notation. 💻 Source Code (C++): https://gist.github.com/shahil020668/...