这类问题有一个通法

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. Vue之创建组件之配置路由!

    Vue之创建组件之配置路由!== 第一步: 当然就是在我们的试图文件夹[views]新建一个文件夹比如home 在home文件夹下面新建一个文件index.vue 第二步:在router目录下做如下事 ...

  2. Python PyCharm安装第三方库

    第一步:在PyCharm菜单中选择File--->Settings 第二步:在设置页出现的搜索框搜索:Project Interpreter 或者直接定位到此处 第三步:输入库名,搜索安装第三方 ...

  3. 利用Mixins扩展类功能

    8.18 利用Mixins扩展类功能 - python3-cookbook 3.0.0 文档 https://python3-cookbook.readthedocs.io/zh_CN/latest/ ...

  4. Filter过滤器除去部分URL链接

    在web.xml中配置的Filter如下: <filter> <filter-name>HazardousParametersFilter</filter-name> ...

  5. OpenStack (云计算与openstck简介)

    云计算 什么是云计算 云计算是一种按使用量付费的模式,这种模式提供可用的,便捷的,按需的网络访问,通过互联网进入可配置的计算资源共享池(资源包括,计算,存储,应用软件和服务) 云计算的特征 易于管理: ...

  6. 深入理解 ProtoBuf 原理与工程实践(概述)

    ProtoBuf 作为一种跨平台.语言无关.可扩展的序列化结构数据的方法,已广泛应用于网络数据交换及存储.随着互联网的发展,系统的异构性会愈发突出,跨语言的需求会愈加明显,同时 gRPC 也大有取代R ...

  7. Flink-v1.12官方网站翻译-P002-Fraud Detection with the DataStream API

    使用DataStream API进行欺诈检测 Apache Flink提供了一个DataStream API,用于构建强大的.有状态的流式应用.它提供了对状态和时间的精细控制,这使得高级事件驱动系统的 ...

  8. Spring MVC 处理一个请求的流程分析

    Spring MVC是Spring系列框架中使用频率最高的部分.不管是Spring Boot还是传统的Spring项目,只要是Web项目都会使用到Spring MVC部分.因此程序员一定要熟练掌握MV ...

  9. 2019牛客暑期多校训练营(第二场)D Kth Minimum Clique(第k团)

    题意:给你n个点 求第k小的团 思路:暴力bfs+bitset压位 #include <bits/stdc++.h> using namespace std; const int N = ...

  10. LIS(nlogn)算法描述//线性DP经典类型

    题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...