121. Best Time to Buy and Sell Stock
June 24, 2025
04:32 AM
No headings found
Loading content...
No headings found
Problem
Bài toán yêu cầu tìm lợi nhuận lớn nhất có thể thu được khi mua và bán cổ phiếu, với điều kiện
Approach
Solution
1function maxProfit(prices: number[]): number {
2 let min_price = Infinity;
3 let max_profit: number = 0;
4 for (const price of prices) {
5 min_price = Math.min(min_price, price);
6 max_profit = Math.max(max_profit, price - min_price)
7 }
8 return max_profit;
9};