Maple's Blog.

Leetcode-121

字数统计: 129阅读时长: 1 min
2019/04/15 Share

maxProfit-链接

实现的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<vector>
#include<cmath>
using namespace std;
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans=0;
for(int i=0;i<prices.size();i++)
{
int m=prices[i];
for(int j=i+1;j<prices.size();j++)
{
if(prices[j]>m)
{
ans=max(prices[j]-m,ans);
}
}
}
return ans;
}
};
int main()
{
vector<int>a={7,6,4,3,1};
cout<<Solution().maxProfit(a)<<endl;
return 0;
}

一道很简单的DP,纯暴力解决了这道题….

CATALOG