Skip to content

Best Time to Buy and Sell Stock III

Find maximum profit with at most two transactions.

InputOutputExplanation
Example 1Result 1Explanation here
Example 2Result 2Explanation here
  • Constraints go here
ApproachTimeSpaceBest For
State Machine DPPrefix-Suffix DPFour Variables DPTBD
★ Recommended

Detailed explanation of approach 1.

⏱ Time O(n) 💾 Space O(n)
function solution():
// Pseudocode here
best-time-to-buy-and-sell-stock-iii_approach1.py
"""
LeetCode #123: Best Time to Buy and Sell Stock III
Approach: {approach}
"""
from typing import List, Dict, Set
def solution({param}) -> {return_type}:
"""
{description}
Time Complexity: O(n)
Space Complexity: O(n)
"""
pass
if __name__ == "__main__":
# Test cases
print(solution({test_input}))
🎯 Interview Favourite

Detailed explanation of approach 2.

⏱ Time O(n) 💾 Space O(n)
best-time-to-buy-and-sell-stock-iii_approach2.py
"""
LeetCode #123: Best Time to Buy and Sell Stock III
Approach: {approach}
"""
from typing import List, Dict, Set
def solution({param}) -> {return_type}:
"""
{description}
Time Complexity: O(n)
Space Complexity: O(n)
"""
pass
if __name__ == "__main__":
# Test cases
print(solution({test_input}))
✓ Simple

Detailed explanation of approach 3.

⏱ Time O(n) 💾 Space O(n)
best-time-to-buy-and-sell-stock-iii_approach3.py
"""
LeetCode #123: Best Time to Buy and Sell Stock III
Approach: {approach}
"""
from typing import List, Dict, Set
def solution({param}) -> {return_type}:
"""
{description}
Time Complexity: O(n)
Space Complexity: O(n)
"""
pass
if __name__ == "__main__":
# Test cases
print(solution({test_input}))