网址: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. openlayer ol.js和ol-debug.js的使用 调试技巧

    二者实现的功能是一样的,有以下区别 : ol.js一般打包项目的时候使用, ol-debug.js编写代码调试的时候使用. 下边是用ol-debug.js编写代码调试的显示: 编写代码调试的技巧,所有 ...

  2. 如何用conda安装软件|处理conda安装工具的动态库问题

    conda的确是一个非常好的工具,对于初学者而言,安装软件就跟用XXX软件管理器一样方便.正因为他如此便利,以至于我介绍如何手动安装工具时,总有人问我为啥不用conda. 我用conda,并且用的很好 ...

  3. 【selenium2】【selenium基础语法】

    #栗子 设置浏览器窗口大小 driver.set_window_size(480,800) #栗子 设置浏览器大小为最大maximize_window() 控制浏览器前进.后退 #栗子from sel ...

  4. _equipment

    该表控制切换地图自动更换装备,离开该地图时,装备自动切换为原来.,HEAD - TABARD小于0时取下装备,等于0时不更换,大于0时更换为对应装备. comment 备注 class 职业索引 1- ...

  5. 副本死亡传送(_instance_die_tele)

    玩家在副本中死亡时,将传送至对应的坐标 mapId 副本地图ID posId 坐标ID,对应_postion中posId

  6. leecode第一百三十六题(只出现一次的数字)

    class Solution { public: int singleNumber(vector<int>& nums) { int len=nums.size(); ; ;i&l ...

  7. python如何判断一个字符串是中文,还是英文。

    参考链接: https://blog.csdn.net/hit0803107/article/details/52885702 decode:  将其它编码转成  ===>unicode enc ...

  8. Top 命令解析

    TOP是一个动态显示过程,即可以通过用户按键来不断刷新当前状态.如果在前台执行该命令,它将独占前台,直到用户终止该程序为止.比较准确的说,top命令提供了实时的对系统处理器的状态监视.它将显示系统中C ...

  9. USBCAN的使用和上位机开发(MFC)

    USBCAN使用手册 参见:https://blog.51cto.com/12572800/2062839 1. USB CAN软件安装与硬件接线 USB CAN是常用的CAN测试工具.它的软件资料存 ...

  10. HTML第十四章总结 HTML forms

    第十四章主要讲了 html forms,通过 forms,我们可以得到 customers' feedback,使得网页能够 interactive,本章的内容分为三个部分: forms 的 elem ...