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 ...
随机推荐
- shell echo -n md5sum使用方法
#!/bin/bash MYSQL='mysql -u*** -p*** -hws5 account' tmp="tmp" resultsource="resultsou ...
- c++多继承浅析
图一 图二 ...
- java比较字符串相等
java中String是对象类型,不能使用"=="比较.正确的用法如下: if(A.equals(B)){ //相等 }
- C语言 · 字符串对比
问题描述 给定两个仅由大写字母或小写字母组成的字符串(长度介于1到10之间),它们之间的关系是以下4中情况之一: 1:两个字符串长度不等.比如 Beijing 和 Hebei 2:两个字符串不仅长度相 ...
- IP数据包格式
IP数据包格式 0 4 8 16 31 |4位版本 | 4位首部长度 | 8位服务类型 | 16位总长度(字节数)| |16位标识 | 3位标志 | 13位片偏移 | |8位生存时间| 8位协议 | ...
- Win10如何显示系统托盘所有图标
最快就是小娜搜索通知 打开Win10“设置”,依次进入“系统 – 通知和操作”,设置界面如图: 点击“选择在任务栏上显示哪些图标”打开如图所示的界面:
- Hadoop Balancer源代码解读
前言 近期在做一些Hadoop运维的相关工作,发现了一个有趣的问题,我们公司的Hadoop集群磁盘占比数值參差不齐,高的接近80%.低的接近40%.并没有充分利用好上面的资源,可是balance的操作 ...
- LintCode - Copy List with Random Pointer
LintCode - Copy List with Random Pointer LintCode - Copy List with Random Pointer Web Link Descripti ...
- Web API(四):Web API参数绑定
在这篇文章中,我们将学习Web API如何将HTTP请求数据绑定到一个操作方法的参数中. 操作方法在Web API控制器中可以有一个或多个不同类型的参数.它可以是基本数据类型或复杂类型.Web API ...
- 常用PHP文件操作函数
注:文件操作函数的行为受到 php.ini 中设置的影响. 当在 Unix 平台上规定路径时,正斜杠 (/) 用作目录分隔符.而在 Windows 平台上,正斜杠 (/) 和反斜杠 (\) 均可使用. ...