public static int maxProfit(int[] prices) {
int minbuy=Integer.MAX_VALUE;
int max=0;
for(int p:prices){
minbuy=Math.min(p,minbuy);
max=Math.max(max,p-minbuy);
}
return max;
}
II.买卖股票二
解法思路:这道题不限制买卖股票次数,求对大利益,动态规划也是简单解决
public int maxProfit(int[]prices) {
if (prices.length == 0) {
return 0;
}
int mny = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i] > prices[i - 1]) {
mny += prices[i] - prices[i - 1];
}
}
return mny;
}