Leetcode: Best Time to Buy and Sell Stock I, II
思路:
1. 算法导论讲 divide and conquer 时, 讲到过这个例子. 书中的做法是先让 price 数组减去一个值, 然后求解最大连续子数组的和. 分治算法的复杂度为 o(nlogn)
2. 剑指 offer 来给出最大连续子数组和的动态规划做法, 时间复杂度缩小到 o(n)
3. 此题不必转化为最大连续子数组和问题, 可以直接使用动态规划解法(我不知道如何转化). 假设 dp[i] 表示第 i 天把股票卖出获得的最大收益, 那么可以根据 price[i] 和 price[i-1] 之间的关系可以推出 dp[i] 与 dp[i-1] 的关系
if(prices[i] > prices[i-1]) // 股票价值升高, 卖出获得的 profit 必然增大
dp[i] = dp[i-1] + prices[i]-prices[i-1];
else { // 股票价格下降, 此时卖出, 有可能亏本. 亏本的话, 就选择第 i 天买, 同日卖, 收益是0
dp[i] = max(0, dp[i-1]+prices[i]-prices[i-1]);
}
总结:
1. 考虑动规解法时, dp 有两种设置方法, 第一中设置方法是 dp[i] 表示前 i 个的最优解, 这是一种全局的设置方法, 最终返回 dp[n]. 第二种是 dp[i] 表示第 i 个的最优解, 这是一种局部的设置方法, 最终返回 optimal(dp[1], dp[2], ...)
2. 代码本来设置了一个数组 dp[20000] 但仍是 runtime error. 后来改成 curDif 和 maxDif 才过
代码:
#include <iostream>
#include <vector>
using namespace std; class Solution {
public: int maxProfit(vector<int> &prices) {
int maxDif = 0;
int curDif = 0; for(int i = 1; i < prices.size(); i ++) {
curDif = max(0, curDif + prices[i]-prices[i-1]);
maxDif = max(curDif, maxDif);
}
return maxDif;
}
};
II
思路:
简单模拟, 自动机
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int maxProfit(vector<int> &prices) {
int profit = 0;
for(int i = 0; i < prices.size(); i ++) {
int initPrice = prices[i];
i++;
while((i) < prices.size() && prices[i] >= prices[i-1] )
i++;
i--;
profit += prices[i]-initPrice;
}
return profit;
}
};
Leetcode: Best Time to Buy and Sell Stock I, II的更多相关文章
- LeetCode:Best Time to Buy and Sell Stock I II III
LeetCode:Best Time to Buy and Sell Stock Say you have an array for which the ith element is the pric ...
- [leetcode]_Best Time to Buy and Sell Stock I && II
一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...
- [LeetCode] Best Time to Buy and Sell Stock with Cooldown 买股票的最佳时间含冷冻期
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock III 买股票的最佳时间之三
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [LeetCode] Best Time to Buy and Sell Stock II 买股票的最佳时间之二
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- [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 ...
- LEETCODE —— Best Time to Buy and Sell Stock II [贪心算法]
Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a ...
- LeetCode Best Time to Buy and Sell Stock IV
原题链接在这里:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/ 题目: Say you have an array ...
随机推荐
- Django---ORM操作大全
前言 Django框架功能齐全自带数据库操作功能,本文主要介绍Django的ORM框架 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计表结构和字段 使用 MyS ...
- How do I create a List in Scala?
Scala List class FAQ: How do I create a List in Scala? You can create a Scala List in several differ ...
- vue2.0的常用功能简介
路由跳转 当我们想要实现点击链接跳转时,可以使用$router来进行跳转 语法如下: '}}) 这里path是要跳转的路径,query里面是路径跳转时要携带的参数,以对象的形式存在 2 获取路由参数 ...
- selenium初探:WebDriverException解决方法探索(以Chrome浏览器|IE浏览器|Edge浏览器为例)
环境参考:win10-64位, python3.6.3, selenium3.7 在初试selenium运行以下代码时 from selenium import webdriver browser = ...
- bootstrap中模态框的大小设置
<!-- 大模态框的调节 --> <button type="button" class="btn btn-primary" data-tog ...
- [DNS]部署局域网DNS服务器
This is a step by step tutorial on how to install and configure DNS server for your LAN using bind9. ...
- Linux Ubuntu下软件包管理
自己整理的一个关于dpkg, apt, aptitude三者的常用命令,方便以后查阅. dpkg: dpkg是用来安装.deb文件,但不会解决模块的依赖关系,且不会关心ubuntu的软件仓库内的软件, ...
- C# cs文件表头模版
设置位置:C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\ItemTemplatesCache\CSharp\Web\2 ...
- node-webkit连接mysql
一.安装node.js mysql驱动库 node-webkit里面没有mysql模块的,我们需要安装mysql模块.我们可以使用npm(Node package manager)进行安装.这里使用到 ...
- loadrunner -56992
设置Run-time Settings ,network speed ,use bandwidth为 512: 回放脚本没有报错,5个并发运行场景报如下错误: vuser_init.c(12): Er ...