https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/
思路:动态规划 找到差值最大的两个数,然后返回最大差值 一般会用到一些函数
C++
采用暴力法,会超时
动态规划
class Solution {
public int maxProfit(int[] prices) {
int[] dp = new int[prices.length];
dp[0]=0;
int minprice=prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i]=Math.max(dp[i-1],prices[i]-minprice);
minprice=Math.min(minprice,prices[i]);
}
return dp[prices.length-1];
}
}
https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/solution/121-mai-mai-gu-piao-de-zui-jia-shi-ji-by-leetcode-/
C++代码实现
LeetCode股票问题总结-java
|