Loading problem...
You are given an integer array prices where prices[i] represents the value of an asset on the i-th time period.
Your objective is to determine the maximum profit achievable by executing exactly one transaction—acquiring the asset at one time period and disposing of it at a later time period.
Return the maximum profit that can be obtained from this single transaction. If no profitable transaction is possible (i.e., the value never increases after any acquisition point), return 0.
Note: You must acquire the asset before you can dispose of it. In other words, the acquisition must occur at an earlier time period than the disposal.
prices = [7,1,5,3,6,4]5Acquire on time period 2 (value = 1) and dispose on time period 5 (value = 6), yielding a profit of 6 - 1 = 5. Note that disposing before acquiring is not permitted.
prices = [7,6,4,3,1]0In this scenario, the asset value continuously decreases over time. Since no profitable transaction exists (every future value is lower than any past value), the maximum profit is 0.
prices = [1,2,3,4,5]4Acquire on time period 1 (value = 1) and dispose on time period 5 (value = 5), yielding a profit of 5 - 1 = 4. This is the optimal strategy when values monotonically increase.
Constraints