DP #121 Best Time to Buy and Sell Stock
Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock
class Solution {
public int maxProfit(int[] prices) {
int n = prices.length;
int mr = prices[n - 1];
int mp = 0;
for (int i = n - 2; i >= 0; i--) {
if (prices[i] < mr) {
int p = mr - prices[i];
mp = Math.max(mp, p);
} else {
mr = prices[i];
}
}
return mp;
}
public int arrSol(int[] prices) {
int n = prices.length;
int mp = 0;
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
int cp = prices[j] - prices[i];
mp = Math.max(mp, cp);
}
}
return mp;
}
}