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 ...
随机推荐
- 剥开比原看代码08:比原的Dashboard是怎么做出来的?
作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...
- kubernetes 实战5_命令_Assign Pods to Nodes&Configure a Pod to Use a ConfigMap
Assign Pods to Nodes how to assign a Kubernetes Pod to a particular node in a Kubernetes cluster. Ad ...
- Visual studio 离线安装
VS2017在下载好安装程序安装的时候,会根据你选择的功能模块来下载所需要的安装程序,而这些安装程序的下载位置并不会让你选择,而是直接放在 C:\ProgramData\Microsoft\Visua ...
- CAS Client集群环境的Session问题及解决方案介绍,下篇介绍作者本人项目中的解决方案代码
CAS Client集群环境的Session问题及解决方案 程序猿讲故事 2016-05-20 原文 [原创申明:文章为原创,欢迎非盈利性转载,但转载必须注明来源] 之前写过一篇文章,介绍单点登 ...
- particles.js使用及配置
particles.js使用及配置 参考:http://blog.csdn.net/csdn_yudong/article/details/53128570 这个项目中有提供demo,可以直接下载这个 ...
- Xshell5中常用linux服务器命令集合
简易版:http://www.zhimengzhe.com/linux/84546.html 详细版:http://www.cnblogs.com/peida/tag/%E6%AF%8F%E6%97% ...
- 堆+建堆、插入、删除、排序+java实现
package testpackage; import java.util.Arrays; public class Heap { //建立大顶堆 public static void buildMa ...
- 关于js函数,方法,对象实例的一些说明
朋友们大家好,好久没有更新文章了,最近正好有空就想着写点什么吧,加上这段时间总是能听到一些朋友们问关于js函数,方法,对象实例到底有什么区别这个问题,所以今天就献丑来简单说明一些吧! 其实这些主要都是 ...
- Mac批量转换mp3为caf
创建一个sh文件,输入如下代码后运行. 遍历文件夹中的mp3然后使用afconvert命令进行转换. #!/bin/bash for i in *.mp3; do afconvert $i " ...
- Python全栈开发-Day4-Python基础4
本节内容 匿名函数 装饰器 列表生成式.迭代器&生成器 内置函数 Json & pickle 数据序列化 1. 匿名函数 匿名函数就是不需要显式的指定函数 1 2 3 4 5 6 7 ...