思路:

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的更多相关文章

  1. 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 ...

  2. [leetcode]_Best Time to Buy and Sell Stock I && II

    一个系列三道题,我都不会做,google之答案.过了两道,第三道看不懂,放置,稍后继续. 一.Best Time to Buy and Sell Stock I 题目:一个数组表示一支股票的价格变换. ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Java – Convert IP address to Decimal Number

    In this tutorial, we show you how to convert an IP address to its decimal equivalent in Java, and vi ...

  2. LeetCode: Permutations II 解题报告

    Permutations II Given a collection of numbers that might contain duplicates, return all possible uni ...

  3. 深入浅出c++之---this指针

    前言:C语言中的数组指针和指针数组 数组指针,是指向数组的指针的缩写:指针数组,是存放指针的数组的缩写.其实很多时候,往往因为简写和缩写带给我们很多困惑.我曾想过不用简称去学习,但在很多时候,我们查询 ...

  4. 基于jQuery点击圆形边框弹出图片信息

    分享一款基于jQuery点击圆形边框弹出图片信息.这是一款鼠标经过图片转换成圆形边框,点击可弹出文字信息.效果图如下: 在线预览   源码下载 实现的代码. html代码: <div id=&q ...

  5. u-boot中网口处理--硬件部分

    1.  网口硬件方案: AT91SAM9G10 + DM9000CEP: DM9000CEP为MAC+PHY解决方案,与MCU链接通过8位或16位数据总线. 内部SRAM为16Kbyte. 2. DM ...

  6. R语言中的数据处理包dplyr、tidyr笔记

    R语言中的数据处理包dplyr.tidyr笔记   dplyr包是Hadley Wickham的新作,主要用于数据清洗和整理,该包专注dataframe数据格式,从而大幅提高了数据处理速度,并且提供了 ...

  7. am335x hid-multitouch.c

    am335x在使用电容屏,需要加载hid-multitouch.ko模块 由下面文件生成 kernel/drivers/hid/hid-multitouch.c 内核中编译模块 make module ...

  8. 74hc165三片级联

    3片74HC165进行级联,用于扩展IO口,读取外界设备的数据. unsigned int read_74165(void) { unsigned ; unsigned ; //三片74hc165,需 ...

  9. Spring 4 官方文档学习(九)数据访问之事务管理

    说明:未整理版,未完待续,请绕行 本部分的重点是数据访问以及数据访问层与业务层之间的交互. 1.Spring框架的事务管理 介绍 http://docs.spring.io/spring/docs/c ...

  10. e1086. if/else语句

    The if statement encloses some code which is executed only if a condition is true. The general synta ...