121. 买卖股票的最佳时机
题目链接:121. 买卖股票的最佳时机 - 力扣(LeetCode)
class Solution {
public int maxProfit(int[] prices) {
int ans = 0;
int min = prices[0];
for(int i = 1; i < prices.length; ++i) {
if(prices[i] > min) {
ans = Math.max(ans, prices[i] - min);
}else {
min = prices[i];
}
}
return ans;
}
}
122.买卖股票的最佳时机II
题目链接:122. 买卖股票的最佳时机 II - 力扣(LeetCode)
class Solution {
public int maxProfit(int[] prices) {
int max = 0;
for(int i = 1; i < prices.length; ++i) {
if(prices[i] > prices[i-1]) {
max += prices[i] - prices[i-1];
}
}
return max;
}
}