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 ...
随机推荐
- Java创建对象的几种方式。
Java创建对象的几种方式(重要): (1) 用new语句创建对象,这是最常见的创建对象的方法. (2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Con ...
- BSEG和BSIS、BSAS、BSID、BSAD、BSIK、BSAK 六个表的关系
BSEG和BSIS.BSAS.BSID.BSAD.BSIK.BSAK六个表的关系 1.数据关系: BSAS+BSIS+BSAK+BSIK+BSAD+BSID = BSEG 2.六个表说明: clear ...
- Appium典型问题处理
1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新版本a ...
- 极简 R 包建立方法--转载
https://cosx.org/2013/11/building-r-packages-easily/ 最近想试一下捣腾一个 R 包出来,故参考了一些教程.现在看到的最好的就是谢益辉大大之前写过的开 ...
- 【Django】【二】模板
1. Django-bootstrap3 guest>python -m pip install django-bootstrap3 [代码] settings.py ""& ...
- ES6中Promise的入门(结合例子)
一.Promise的前言 解决回调地狱 //以往回调方式 函数1(function(){ //代码执行...(ajax1) 函数2(function(){ //代码执行...(ajax2) 函数3(f ...
- Codeforces 729E Subordinates
题目链接:http://codeforces.com/problemset/problem/729/E 既然每一个人都有一个顶头上司,考虑一个问题: 如果这些人中具有上司数目最多的人有$x$个上司,那 ...
- activity 运行流程
图1 图2 图3 图四
- Ubuntu ls: cannot open directory .: Permission denied
把该目录赋予权限: sudo chmod xxx
- Java String删除字符串中间的某部分
当你想删除字符串中的某部分时,java中并没有直接提供相关的方法,比如想删除 "cout<<\"Hello world\"<<endl" ...