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 ...
随机推荐
- 重装Ubuntu系统
1.安装JDK参考:http://weixiaolu.iteye.com/blog/1401786jdk-6u31-linux-i586.bin莫名奇妙的安装失败.所以下载了jdk-7u45-linu ...
- 标准库string的用法探讨
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必 担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至 ...
- 7款基于jquery的动画搜索框
无论是电商网站,还是媒体网,还是个人博客,每个网站都有属于自己个性化的搜索框.今天小编给大家带来7款基于jquery的动画搜索框.每个搜索框都采用了动画效果,一起看下效果图吧. 在线预览 源码下载 ...
- 阿里云ECS,WampServer无法访问外网
情况: 使用阿里云ECS服务器.服务端打开WampServer后,在服务端能通过127.0.0.1和localhost访问到WampServer的首页. 阿里云已经给了外网IP,不需要路由器再做端口映 ...
- 3DS更新R4烧录卡内核
机子是N3DSLL,用的R4烧录卡是银卡HK版. 关于R4烧录卡的基础知识科普贴: https://tieba.baidu.com/p/4855297365 为了防止该网页挂掉还是存图吧. 找最新内核 ...
- jQuery 选择器大全总结
jQuery基础语法中规定的选择器有三种,分别是类选择器.ID选择器.标签选择器.如:$(“.aa”).$(“#id”).$(“div”),但中实际的应用中,DOM机构非常复杂,层级非常多.如和应对这 ...
- java资料——数据结构(转)
数据结构 (计算机存储.组织数据方式) 数据结构是 ...
- spring配置事务 元素 "tx:annotation-driven" 的前缀 "tx" 未绑定
在进行spring与mybatis整合时,启动项目报错,控制台提示“元素 "tx:annotation-driven" 的前缀 "tx" 未绑定”. 经过查找, ...
- JAVA虚拟机、Dalvik虚拟机和ART虚拟机简要对比
1.什么是JVM? JVM本质上就是一个软件,是计算机硬件的一层软件抽象,在这之上才能够运行Java程序,JAVA在编译后会生成类似于汇编语言的JVM字节码,与C语言编译后产生的汇编语言不同的是, ...
- Collections 集合工具类
集合工具类 包括很多静态方法来操作集合list 而Collections则是集合类的一个工具类/帮助类,其中提供了一系列静态方法,用于对集合中元素进行排序.搜索以及线程安全等各种操作. 1) 排序( ...