Leetcode122 - Best Time to Buy and Sell Stock II (dp)
来源:互联网 发布:免费销售软件 编辑:程序博客网 时间:2024/05/19 15:21
题意
给定一个股票的价格序列,你可以进行多次交易,在一天选择买入并在之后的某一天卖出,求能获得的最大利润。
要求:当买入一个股票时候不能买入新的股票
思路
一个多阶段决策问题,我们用
代码
const int maxn = 100000 + 5;int d[maxn][2];class Solution {public: int maxProfit(vector<int>& a) { int n = a.size(); if (n <= 1) return 0; d[0][0] = 0, d[0][1] = -a[0]; for (int i = 1; i < n; i++) { d[i][0] = max(d[i - 1][0], d[i - 1][1] + a[i]); d[i][1] = max(d[i - 1][1], d[i - 1][0] - a[i]); } return d[n - 1][0]; }};
0 0
- leetcode122.[DP] Best Time to Buy and Sell Stock II
- Leetcode122 - Best Time to Buy and Sell Stock II (dp)
- leetcode122---Best Time to Buy and Sell Stock II
- LeetCode122:Best Time to Buy and Sell Stock II
- leetcode122 Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- leetcode122 Best Time to Buy and Sell Stock II
- Leetcode122. Best Time to Buy and Sell Stock II
- LeetCode122—Best Time to Buy and Sell Stock II
- leetcode122. Best Time to Buy and Sell Stock II
- leetcode122~Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II题解
- LeetCode122 Best Time to Buy and Sell Stock II
- Leetcode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122. Best Time to Buy and Sell Stock II
- LeetCode122——Best Time to Buy and Sell Stock II
- anaconda没法更新scikit-learn
- GreenOpenCsharpWarper(GOCW)
- Actvity启动过程涉及到的数据结构
- php学习笔记--关于时间戳
- python初学七 list
- Leetcode122 - Best Time to Buy and Sell Stock II (dp)
- 代码对比工具winMerge
- 正则化方法:L1和L2 regularization、数据集扩增、dropout
- Hadoop学习笔记-002-CentOS_6.5_64_克隆系统后更改IP及主机名
- LeetCode 460. LFU Cache
- 再探完全背包 HDOJ 2069 Coin Change
- 乒乓球
- JNI全局引用与JFrame.dispose()方法
- java的getClass()函数