动态规划:面试题63. 股票的最大利润
面试题63. 股票的最大利润
动态规划解析:
1 class Solution { 2 public: 3 int maxProfit(vector<int>& prices) 4 { 5 if (prices.size() < 2) 6 { 7 return 0; 8 } 9 int cout = prices[0]; 10 // int dp[prices.size()]; 11 // dp[0] = 0; 12 int profit = 0; 13 for (int i=1; i<prices.size(); i++) 14 { 15 cout = min(cout, prices[i]); 16 profit = max(profit, prices[i]-cout); 17 //dp[i] = max(dp[i-1], prices[i]-cout); 18 } 19 return profit; 20 } 21 };