121. Best Time to Buy and Sell Stock 买卖股票的最佳时机
网址:https://leetcode.com/problems/Best-Time-to-Buy-and-Sell-Stock/
第一想法是滑动窗口法,稍微尝试后发现不可行,至少我不会。。。
而后想到动态规划,进一步思考发现完全可行:
| index | 0 | 1 | 2 | 3 | 4 | 5 |
| price[] | 7 | 1 | 5 | 3 | 6 | 4 |
| dp[] | 0 | 0 | 4 | 2 | 5 | 3 |
| price[index-1] - dp[index-1] | 0 | 0 | 1 | 1 | 1 | 1 |
状态:dp[i] 表示prices[]从 0 到 i 这部分的最大利润
状态转移方程:
dp[i] = max(0, prices[i] - (prices[i-1] - dp[i-1]));
使用dp数组的代码如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
int ans = ;
vector<int> dp(prices.size(), );
for(int i=; i<prices.size();i++)
{
dp[i] = max(, prices[i] - (prices[i-] - dp[i-]));
ans = max(dp[i], ans);
}
return ans;
}
};
然而,dp数组会占用多余的空间。我们知道,一位dp问题的dp数组的赋值往往可以转化为几个变量之间的循环赋值的过程
所以,改进如下:
class Solution {
public:
int maxProfit(vector<int>& prices) {
if(prices.size() == )
{
return ;
}
int ans = ;
int res = ;
int pre_price = prices[];
int pre_dp = ;
for(int price : prices)
{
res = max(, price - (pre_price - pre_dp));
pre_dp = res;
pre_price = price;
ans = max(res, ans);
}
return ans;
}
};
注意,用这种方式要先判断size是否为0,否则会出现Runtime Error。。。
两份代码的Runtime都是8ms,而改进后内存占用由9.7MB减少到9.5MB,千万别小看这0.2MB。
我们从击败5%的同学升级到击败46%的同学!

121. Best Time to Buy and Sell Stock 买卖股票的最佳时机的更多相关文章
- [LeetCode] 121. Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. Best Time to Buy and Sell Stock买卖股票12
一 [抄题]: If you were only permitted to complete at most one transaction (ie, buy one and sell one sha ...
- [Leetcode] Best time to buy and sell stock 买卖股票的最佳时机
Say you have an array for which the i th element is the price of a given stock on day i. If you were ...
- LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)
题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- [LintCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 121. Best Time to Buy and Sell Stock(股票最大收益)
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 【leetcode】121. Best Time to Buy and Sell Stock(股票问题)
You are given an array prices where prices[i] is the price of a given stock on the ith day. You want ...
- 121. Best Time to Buy and Sell Stock (一) leetcode解题笔记
121. Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of ...
随机推荐
- Ubuntu14.04 clang3.8 Installation Guide
Reference Installing clang 3.8 on Ubuntu 14.04.3. Ubuntu14.04 clang3.8 Installation Guide 1.add the ...
- Sublime Text 查找时排除指定的文件夹或文件
Sublime Text 查找时排除指定的文件夹或文件 Ctrl + Shift + F这组快捷键可以调出 Sublime Text 的查找替换窗口,里边有一栏 Where,可以做一些高级设置:d:\ ...
- gawk命令详解
GNU awk: sort.cut.uniq.wc等参考: https://blog.csdn.net/lk07828/article/details/46324807 https://blog.cs ...
- 【Java】【THINK】
1. 新建类,应优先考虑“组织”对象,而不是继承.这样可以保持清爽. 2. Java对象&对象句柄: 声明了一个类型的变量也就是声明了一个该类型的对象.但是这个对象只是个抽象的概念,并不会在内 ...
- async函数对比Generator函数
首先定义一个读取文件的异步函数 var readFile = function(fileName){ return new Promise((resolve,reject)=>{ fs.read ...
- 前端分页插件bootstrapPaginator的使用
引入bootstrap-paginator.js <table class="table table-striped table-bordered table-hover dataT ...
- Tp3.2 复合查询
我们常常有这样的需求,比如搜索. 搜索出,标题,子标题,内容中包含某某关键字. 这就要and,or结合使用了. $where = ['is_show'=>1,'status'=>1]; / ...
- 清除div重叠浮动的方法
在最后加入这样一个div. 并且为div加入以下属性: .clear { clear:both;}
- sublime text---注释
Sublime在进行前端开发时非常棒,当然也少不了众多的插件支持,DocBlocker是在Sublime平台上开发一款自动补全代码插件,支持JavaScript (including ES6), PH ...
- HeadFIrst Ruby 第七章总结 hashes
前言 这一章节介绍了 Ruby 中 hash 这一数据类型的用法和特征. Hash 的定义 与 array 的对比 最大的不同: An array can only use integers as i ...