Say you have an array for which the i th element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
import java.lang.Math;
public class Solution {
public int maxProfit(int[] prices) {
if(prices==null||prices.length==0)//这里必须考虑特殊情况
return 0;
int max_res=0;
int min=prices[0];
for(int i=1;i<prices.length;i++)
{
if(prices[i]<min)
min = prices[i];
else
max_res = Math.max(prices[i]-min,max_res);
}
return max_res;
}
}