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 ...
随机推荐
- 小tip: 如何让contenteditable元素只能输入纯文本
div模拟textarea文本域轻松实现高度自适应 这篇文章发布于 2010年12月23日,星期四,22:07,归类于 css相关. 阅读 112630 次, 今日 40 次 by zhangxinx ...
- SimpleDateFormat转换时间,12,24时间格式
Date d = new Date(); SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");//1 ...
- 【转】Mysql两种存储引擎的异同【MyISAM和InnoDB】
MySQL默认采用的是MyISAM. MyISAM不支持事务,而InnoDB支持.InnoDB的AUTOCOMMIT默认是打开的,即每条SQL语句会默认被封装成一个事务,自动提交,这样会影响速度,所以 ...
- eclipse javaee 插件安装
eclipese 精简版安装java ee插件 , 按图走 (eclipse 版本 : Indigo Service Release 1 (3.7.1)) java ee 在线安装地址: htt ...
- layer返璞归真
返璞归真 前端社区正在变得日渐喧嚣,我们似乎很难停下追逐的脚步.而Layui偏偏回望当初,奔赴在返璞归真的漫漫征途,自信并勇敢着,追寻于原生态的书写指令,试图以最简单的方式诠释高效
- Clock函数用法
clock()是C/C++中的计时函数,而与其相关的数据类型是clock_t.在MSDN中,查得对clock函数定义如下: clock_t clock(void) ; 这个函数返回从“开启这个程序进程 ...
- Oracle 外键约束子表、父表
CREATE TABLE employees( employee_id NUMBER(6), last_name VARCHAR2(25) NOT NULL, ...
- java资料——顺序存储结构和链式存储结构(转)
顺序存储结构 主要优点 节省存储空间,随机存取表中元素 缺 点 插入和删除操作需要移动元素 在计算机中用一组地址连续的存储单元依次存储线性表的各个数据元素,称作线性表的顺序存储结构. 顺序存储结 ...
- [转]RabbitMQ,ActiveMQ,ZeroMQ,Kafka之间的比较与资料汇总
MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka.这几种MQ到底应该选择哪个?要根据自己项目的业务场景和需求.下面我列出这些MQ之间的对比数据和资料. 第一部 ...
- spring boot之访问静态页面
楼主前两天自学spring boot,然后在学习的过程中,出现一个疑问,就是如何去访问静态的html网页,这个问题,楼主上网上搜了下,找到的是在resource目录下建立一个templates文件夹, ...