122. Best Time to Buy and Sell Stock II
来源:互联网 发布:端口映射软件哪个好 编辑:程序博客网 时间:2024/06/04 19:10
动态规划,dp[i]=max(dp[i-1],dp[k]+prices[i]-nums[k+1]),k+1>=0&&k+1<=i-1;
class Solution {public: int maxProfit(vector<int>& prices) { int n=prices.size(); if(n==0) return 0; int i; int B=-prices[0]; int dp[n]; dp[0]=0; for(i=1;i<n;i++) { B=max(B,dp[i-1]-prices[i]); dp[i]=max(dp[i-1],B+prices[i]); } return dp[n-1]; }};
可以用贪心法;
class Solution {public: int maxProfit(vector<int>& prices) { int profit=0; int n=prices.size(); for(int i=1;i<n;i++) { int dif=prices[i]-prices[i-1]; if(dif>0) profit+=dif; } return profit; }};
0 0
- 122. Best Time to Buy and Sell Stock II
- LeetCode 122. Best Time to Buy and Sell Stock II
- 122.Best Time to Buy and Sell Stock II
- [Leetcode] 122. Best Time to Buy and Sell Stock II
- [leetcode] 122.Best Time to Buy and Sell Stock II
- 【leetcode】122.Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 【LeetCode】122.Best Time to Buy and Sell Stock II
- [leetcode] 122. Best Time to Buy and Sell Stock II
- LeetCode #122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II LeetCode
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- [LeetCode]122. Best Time to Buy and Sell Stock II
- 122. Best Time to Buy and Sell Stock II
- Leetcode 122. Best Time to Buy and Sell Stock II
- leetcode 122. Best Time to Buy and Sell Stock II
- 《JAVA与模式》之享元模式
- PAT乙级.1043. 输出PATest(20)
- Linux运维笔记----用户管理
- linux系统链接---软链接
- test
- 122. Best Time to Buy and Sell Stock II
- jQuery基础(上)
- Eclipse 智能提示,改字体大小,更改编码,绑定源码等等
- Framework 重装
- jsp的<jsp:setProperty>动作元素的value和param属性的区别。
- js修改html的样式
- 《深度探索C++对象模型》—Function语意学(The Semantics of Function)
- 【用脚趾头解决NOIP】(1):NOIP2010~NOIP2015普及组问题求解
- JZOJ 4740 捕猫计划