一、题目内容
二、题解
解法1:
思路
dp[i] = Max(dp[i-1],prices[i]-Min(prices[0-i]))
代码
public class Solution {
/**
*
* @param prices int整型一维数组
* @return int整型
*/
public int maxProfit (int[] prices) {
// write code here
//dp[i] = Max(dp[i-1],prices[i]-Min(prices[0-i]))
int maxProfit = Integer.MIN_VALUE;
int minCost = Integer.MAX_VALUE;
for(int i = 0;i<prices.length;i++){
minCost = Math.min(minCost,prices[i]);
maxProfit = Math.max(maxProfit,prices[i]-minCost);
}
return maxProfit;
}
}
