题解 | #买卖股票的最好时机(三)#
买卖股票的最好时机(三)
https://www.nowcoder.com/practice/4892d3ff304a4880b7a89ba01f48daf9
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 两次交易所能获得的最大收益 * @param prices int整型一维数组 股票每一天的价格 * @param pricesLen int prices数组长度 * @return int整型 */ int maxProfit(int* prices, int pricesLen ) { // write code here int b1 = - * prices, s1 = 0, b2 = - * prices, s2 = 0; for (int i = 1; i < pricesLen; i++) { s2 = b2 + prices[i] > s2 ? b2 + prices[i] : s2; b2 = s1 - prices[i] > b2 ? s1 - prices[i] : b2; s1 = b1 + prices[i] > s1 ? b1 + prices[i] : s1; b1 = - prices[i] > b1 ? - prices[i] : b1; } return s2; }