122. Best Time to Buy and Sell Stock II
Tags:
Medium
Skills:
Array
June 24, 2025
04:32 AM
No headings found
Loading content...
Related Posts
Leetcode
No headings found
Related Posts
Leetcode
Problem
Bài tập này yêu cầu bạn tìm lợi nhuận tối đa khi được phép thực hiện nhiều giao dịch mua vá bán cổ phiếu, nhưng không được nắm giữ nhiều hơn 1 cổ phiếu tại một thời điểm (phải bán trước khi mua tiếp). Bạn có thể mua và bán trong cùng một ngày
Approach
Solution
1function maxProfit(prices: number[]): number {
2 let profit = 0;
3 for (let i = 1; i < prices.length; i++) {
4 if (prices[i] > prices[i - 1]) {
5 profit += prices[i] - prices[i - 1]
6 }
7 }
8 return profit
9};