思路:最大利润=max(当前价格-历史最小价格)
import java.util.*; public class Solution { /** * * @param prices int整型一维数组 * @return int整型 */ public int maxProfit (int[] prices) { if(prices==null||prices.length<2){ return 0; } int minPrices = prices[0]; int maxProfit = 0; for(int i=0; i<prices.length; i++){ if(prices[i]<minPrices){ minPrices = prices[i]; } int profit = prices[i]-minPrices; if(profit>maxProfit){ maxProfit=profit; } } return maxProfit; } }
![]()