题目链接:LeetCode121. 买卖股票的最佳时机题解1:无脑暴力,复杂度O(N^2) class Solution { public int maxProfit(int[] prices) { /*暴力解法*/ int mp = 0,len = prices.length; for(int i = 0; i < len - 1; ++i) { for (int j = i + 1; j < len; ++j) { if(prices[j] - prices[i] > mp) { mp = prices[j] - prices[i]; } } } return mp; } } 题解2:使用dp,状态转移方程:res = max(res,prices[i] - min_val). class Solution { public int maxProfit(int[] prices) { // minValue:前i天的最小买入 int res = 0,minValue = Integer.MAX_VALUE; for(int i = 0; i < prices.length; ++i){ minValue = Math.min(minValue,prices[i]); res = Math.max(res,prices[i] - minValue); } return res; } } 性能比较