首页 > 代码库 > Best Time to Buy and Sell Stock (java)

Best Time to Buy and Sell Stock (java)

有一组数组代表股票的价格

一次买一次卖如何得到最大利润?

 1  public int maxProfit(int[] prices) { 2          if(prices.length==0)return 0; 3          int maxProfit=0; 4          int min=prices[0]; 5          for(int i=0;i<prices.length;i++) 6          { 7              maxProfit=prices[i]-min>maxProfit?prices[i]-min:maxProfit; 8              min=prices[i]<min?prices[i]:min; 9          }10          11          return maxProfit;12          13          14      }

 

Best Time to Buy and Sell Stock (java)