Skip to content

Practice Best Time to Buy and Sell Stock III

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#123
LeetCode Problem

Best Time to Buy and Sell Stock III

Problem Statement

You are given an array prices where prices[i] is the price of a given stock on the ith day. Find the maximum profit you can achieve. You may complete at most two transactions. Note: You may not engage in multiple transactions simultaneously (i.e., you must sell the stock before you buy again).

Source: Best Time to Buy and Sell Stock III on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
prices = [3,3,5,0,0,3,1,4]
Output
6

# Example 2

Input
prices = [1,2,3,4,5]
Output
4

# Example 3

Input
prices = [7,6,4,3,1]
Output
0

Constraints

1 <= prices.length <= 10^5
0 <= prices[i] <= 10^5

Related LeetCode Problems

Practice more problems similar to Best Time to Buy and Sell Stock III.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.