# Is something wrong with the test case?

• Input: [6,1,3,2,4,7]
Output: 5
Expected: 6

• ``````public int maxProfit(int[] prices) {
if (prices.length <= 1) return 0;

int profit = 0;
int min = prices[0];
for (int i = 0; i < prices.length; i++) {
min = Math.min(min, prices[i]);
profit = Math.max(profit, prices[i] - min);
}
return profit;
}
``````

buy the stock at 1 (prices[1]) and sell it at 7 (prices[4]). so the profit would be 6 instead of 5

• this is really neat

• My solution was too overkill.
This is a simple and elegant solution. I like it!

• But if you buy the stock at 1 and sell it at 3, then buy it again at 2 and sell it at 7, isn't the profit=(3-1) + (7-2)=7 being larger than 6?

• The limitation is "at most one transaction".

• “you were only permitted to complete at most one transaction” in your case, two transactions are processed

• yes, but in the solution, every time I calculate the `min` and `profit`, it doesn't mean the stock is sold

• this one is awesome.
It's some kind of DP.

• this is awesome. just one thing, should we do the null check for prices?

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.