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 slow = 0 ;//指向买入点
int fast = 1 ;//寻找卖出最佳点
int len = prices.length ;
int maxProfit = 0 ;//最大利益
while(fast < len) {//思想就是:贪心,每次都是在价格最低点买入,在价格最高点卖出
if(prices[fast] > prices[fast-1]) {
fast ++ ;
if(fast == len) {
maxProfit += prices[fast-1] - prices[slow] ;
}
} else {
maxProfit += prices[fast-1] - prices[slow] ;
slow = fast ;
fast ++ ;
}
}
return maxProfit ;
}
}