网址: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. Kubernetes之Controllers一

    ReplicaSet is the next-generation Replication Controller. The only difference between a ReplicaSet a ...

  2. Writing device drivers in Linux: A brief tutorial

    “Do you pine for the nice days of Minix-1.1, when men were men and wrote their own device drivers?”  ...

  3. 【Ruby】【遇到的问题】

    1 Error fetching https://gems.ruby-china.org/: certificate verify failed (https://gems.ruby-china.or ...

  4. JavaScript重点知识(一)

    一.总括 基础知识: 1.变量 2.原型和原型链 3.作用域和闭包 4.异步和单线程 JS的API: 1.BOM,DOM操作 2.事件绑定 3.Ajax 4.JSOP 5.存储 二.基础知识 2.1知 ...

  5. mariaDB中文乱码

    cetos7 下 http://hongjun.blog.51cto.com/445761/400985 1 .  copy 一个文件成 /etc/my.cnf cp /usr/share/mysql ...

  6. ubuntu14.04 Keras框架搭建

    >>>sudo su >>> pip3 install -U --pre pip setuptools wheel >>> pip3 instal ...

  7. Jmeter干货 不常用却极其有用的几个地方

    1. Jmeter测试计划下Run Thread Groups consecutively 表示序列化执行测试计划下所有线程组中的各个请求 如下图配置,新建的测试计划中,不默认勾选此项, 而享用Jme ...

  8. CentOS6.5下搭建VNC服务器

    VNC(Virtual Network Computing,虚拟网络计算机)是一款由AT&T欧洲研究实验室开发的远程控制软件,允许用户在网络的任何地方使用简单的程序来和一个特定的计算机进行交互 ...

  9. 牛客网NOIP赛前集训营-普及组(第一场)C 括号

    括号 思路: dp 状态:dp[i][j]表示到i位置为止未匹配的 '(' 个数为j的方案数 状态转移: 如果s[i] == '(' dp[i][j] = dp[i-1][j] + dp[i-1][j ...

  10. Codeforces 377A - Maze

    A. Maze 题目链接:http://codeforces.com/contest/377/problem/A time limit per test 2 seconds memory limit ...