提交时间:2024-03-02 11:00:20
运行 ID: 133959
#include <iostream> #include <vector> using namespace std; int maxProfit(vector<int>& prices) { int n = prices.size(); int ans = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { ans = max(ans, prices[j] - prices[i]); } } return ans; } int main() { vector<int> prices = { 7,1,5,3,6,4 }; cout << "The prices: "; for(int i = 0; i < prices.size(); i++) { cout << prices[i] << " "; } cout << endl; int ans = maxProfit(prices); cout << "The max profit: " << ans << endl; return 0; }