动态规划:面试题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 };

 

posted @ 2020-04-10 10:33  Ternence_zq  阅读(251)  评论(0编辑  收藏  举报