solution1 : 经典双指针
public class Solution {
public int maxProfit(int prices[]) {
int minprice = Integer.MAX_VALUE;
int maxprofit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minprice) {
minprice = prices[i];
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
}
}
return maxprofit;
}
}
本文介绍了一种解决股票买卖问题的经典算法——双指针法,通过遍历股票价格数组,找到最低价和当前价与最低价的差值,以求得在给定价格序列中进行买卖操作的最大利润。
431





