121. 买卖股票的最佳时机
class Solution { public int maxProfit(int[] prices) { int ans=0; for(int i=0;i<prices.length;i++){ int start=prices[i]; int end=prices[i]; for(int j=i+1;j<prices.length;j++){ if(prices[j]>end){ end=prices[j]; } } if(end-start>ans){ ans=end-start; } } return ans; } }
class Solution { public int maxProfit(int[] prices) { int ans=0; if(prices.length>0){ int min=prices[0]; for(int i=0;i<prices.length;i++){ if(prices[i]<min){ min=prices[i]; } if(prices[i]-min>ans){ ans=prices[i]-min; } } } return ans; } }