网址: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 买卖股票的最佳时机的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. LeetCode Best Time to Buy and Sell Stock 买卖股票的最佳时机 (DP)

    题意:给定一个序列,第i个元素代表第i天这支股票的价格,问在最佳时机买入和卖出能赚多少钱?只买一次,且仅1股,假设本钱无限. 思路:要找一个最低价的时候买入,在最高价的时候卖出利润会最大.但是时间是不 ...

  5. [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 ...

  6. [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 ...

  7. 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 ...

  8. 【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 ...

  9. 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 ...

随机推荐

  1. 微服务架构与实践3_api

    场景分析 描述产品服务,基于REST的接口 表述性状态转移(Representational State Transfer,REST) 任务拆分 将整体要做的工作内容划分成小的任务: 统一时间聚焦一个 ...

  2. facebook api之Business Manager API

    Business-scoped Users - The new user is tied to a particular business and has permissions scoped to ...

  3. Footnotes for tables in latex - 为latex的table加上footnotes

    参考: Footnotes for tables in LaTeX Footnote in tabular environment Footnotes for tables in latex - 为l ...

  4. 将 Graphviz .dot 文件转换为其他格式的图像

    参考: Graphviz: How to go from .dot to a graph? 将 Graphviz .dot 文件转换为其他格式的图像 在Linux系统下,使用以下命令: dot -Tp ...

  5. Java 静态方法不能重写但可以被子类静态方法覆盖

    强调 静态方法是属于类的,只存在一份,会被该类的所有对象共享.不可以被重写. 静态方法可以被子类继承,但是不可以被子类重写 class door{ } class wood_Door extends ...

  6. 使用git上传项目到github的最基础命令

    一.前言 把github作为自己项目托管的地方,实在是一个明智的选择.就算你不为自己项目考虑,你也要为你团队项目开发而学呀!可能有些初学者(比如我)会觉得git命令好多啊,又是各种术语,觉得好难上手. ...

  7. header 格式

    headers = { 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,* ...

  8. Android自定义权限

    一.自定义权限 自定义权限,一般是考虑到应用共享组件时的安全问题.我们知道在四大组件 AndroidManifest 中注册的时候,添加 exported = "true" 这一属 ...

  9. 关于Java类和包的那些事

    *.Java文件 问题:一个.java源文件中是否可以包括多个类(不是内部类)?有什么限制? 答案:可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致.一个文件中可以 ...

  10. 力扣(LeetCode)258. 各位相加

    给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数. 示例: 输入: 38 输出: 2 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2. 由于 2 是一位数,所 ...