121. Best Time to Buy and Sell Stock (Array)

找最小的价格更新 遍历一次就可以

99%:

 1 class Solution {
 2     public int maxProfit(int[] prices) {
 3         int maxP = 0;
 4         int min = Integer.MAX_VALUE;
 5         for(int i = 0; i < prices.length; i++) {
 6             if(prices[i] < min) {
 7                 min = prices[i];
 8             }else {
 9                 if(prices[i] - min > maxP) {
10                     maxP = prices[i] - min;
11                 }
12             }
13         }
14         return maxP;
15     }
16 }

猜你喜欢

转载自www.cnblogs.com/goPanama/p/9363620.html
今日推荐