原题地址
https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/
题意
给定数组prices[],找出i,j(i<j)使得prices[j]-prices[i]最大
思路
就是扫一遍数组,不断更新出现的最小值和和最小值的最大差值了 (并没意识到这个也算动态规划
代码
class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
if(n==0) return 0;
int result =0;
int low=prices[0];
for(int i =1;i<n;i++){
int temp = prices[i]-low;
if(temp<0){
low = prices[i];
}else{
if(temp>result){
result = temp;
}
}
}
return result;
}
};