这类问题有一个通法

https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-iii/solution/yi-ge-tong-yong-fang-fa-tuan-mie-6-dao-gu-piao-wen/

再说吧,该睡觉了

122

只要是增加的买就完事了

class Solution {
public:
int maxProfit(vector<int>& prices) {
int max = 0 ;
if(prices.size() == 0) return 0;
for(int i = 0 ; i < prices.size() -1; i++){
if(prices[i+1] - prices[i] > 0){
max += prices[i+1] - prices[i];
}
}
return max;
}
};

123

简单的思路;

因为限制了买卖间必须间隔,所以遍历一遍,构建状态,然后载遍历一遍就好了

class Solution {
public:
int maxProfit(vector<int>& prices) {
int n = prices.size();
int result = 0;
// right[i] 代表从 i 开始的 prices 序列,能赚的最大的钱
vector<int> right = vector(n,0);
int maxRight = 0;
int resRight = 0;
for(int i=n-1;i>=0;i--) {
// 倒序遍历,动态规划得到 right 数组
maxRight = max(prices[i], maxRight);
right[i] = max(resRight, maxRight - prices[i]);
}
// 正序遍历,resLeft 代表截止到 i,prices 序列能赚的最大的钱
int minLeft = INT_MAX;
int resLeft = 0;
for(int i=0;i<n;i++) {
minLeft = min(prices[i], minLeft);
resLeft = max(resLeft, prices[i] - minLeft);
// resLeft + right[i] 代表以 i 为分割,两侧分别能赚到的钱的总和
result = max(result, resLeft + right[i]);
}
return result;
}
};

leetcode 122 123 309 188 714 股票买卖 动态规划的更多相关文章

  1. Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力)

    Leetcode 413. Arithmetic Slice 算术序列切片(动态规划,暴力) 题目描述 如果一个数组1.至少三个元素2.两两之间差值相同,那么这个数组就是算术序列 比如下面的数组都是算 ...

  2. Leetocode7道买卖股票问题总结(121+122+123+188+309+901+714)

    题目1----121. 买卖股票的最佳时机I: 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/ 给定一个数组, ...

  3. 121. 122. 123. 188. Best Time to Buy and Sell Stock *HARD* 309. Best Time to Buy and Sell Stock with Cooldown -- 买卖股票

    121. Say you have an array for which the ith element is the price of a given stock on day i. If you ...

  4. 领扣-121/122/123/188 最佳买卖时机 Best Time to Buy and Sell MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. [LeetCode]121、122、309 买股票的最佳时机系列问题(DP)

    121.买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意 ...

  6. leetcode 121 122 123 . Best Time to Buy and Sell Stock

    121题目描述: 解题:记录浏览过的天中最低的价格,并不断更新可能的最大收益,只允许买卖一次的动态规划思想. class Solution { public: int maxProfit(vector ...

  7. LeetCode No.121,122,123

    No.121 MaxProfit 买卖股票的最佳时机 题目 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你 ...

  8. [LeetCode] 122. Best Time to Buy and Sell Stock II 买卖股票的最佳时间 II

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  9. 【Leetcode】1340. Jump Game V 【动态规划/记忆性搜索】

    Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + ...

随机推荐

  1. 修改conda和pip源

    修改conda源为中科大源 Windows修改C:\Users\user(user替换为当前登陆系统的用户)目录下的.condarc文件 Linux修改家目录下的.condarc文件 channels ...

  2. 词嵌入之GloVe

    什么是GloVe GloVe(Global Vectors for Word Representation)是一个基于全局词频统计(count-based & overall statisti ...

  3. 关于Mysql数据库建库字符集utf8mb4下,排序规则utf8mb4_bin和utf8mb4_general_ci选择造成的查询匹配大小写问题

    场景描述: 项目采用了分库模式进行不同业务的开发,在共有的功能模块进行设计的时候采用主从库,或者各分库之中存在同样的库表结构,在使用过程中做库表同步的时候一定要保证库表所在的数据库的字符集和编码格式是 ...

  4. Mybatis【15】-- Mybatis一对一多表关联查询

    注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-11-one2one,需要自取,需要配置maven ...

  5. BeetleX大数据之产品分析服务

        数据规模过于庞大?数据标签过多难以管理?增加新的分析维度需要配置?这些beetlex.io都能轻松解决,即导即用,数据标签自动管理,轻易实现多种维度数据分析处理.接下介绍BeetleX针对产品 ...

  6. ovsdb-client命令

    ovsdb-server 的命令行接口. 查看有哪些数据库: ovsdb-client list-dbs [server] 查看数据库 schema: ovsdb-client get-schema ...

  7. 解决 win10 无法安装VS2019,visual studio installer下载进度始终为0

    解决 win10 无法安装VS2019,visual studio installer下载进度始终为0 目录 解决 win10 无法安装VS2019,visual studio installer下载 ...

  8. MySQL调优之查询优化

    一.查询慢的原因 1.网络 (1)网络丢包,重传 这个比较容易理解.当SQL 从客户端发送到数据库,执行完毕,数据库将结果返回给客户端,这个将数据返回给客户端的过程本质是网络包传输.因为链路的不稳定性 ...

  9. Vue整合swiper报错Could not compile template .....swiper\dist\css\swiper.css解决办法

    问题描述 今天做一个前端项目,安装幻灯片插件vue-awesome-swiper后 运行npm run dev 后报错如下: `ERROR Could not compile template E:\ ...

  10. 游标 深度分页 deep paging

    Solr Deep Paging(solr 深分页) - ickes的专栏 - CSDN博客 https://blog.csdn.net/xl_ickes/article/details/427725 ...