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

Design an algorithm to find the maximum profit. You may complete at most two transactions.

Note: You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

C++

class Solution {
public:
int maxProfit(vector<int>& prices){
int buy1 = INT_MIN;
int sell1 = 0;
int buy2 = INT_MIN;
int sell2 = 0;
for(int i = 0; i< prices.size(); i++){
buy1 = max(buy1,-prices[i]);
sell1 = max(sell1,buy1 + prices[i]);
buy2 = max(buy2, sell1 - prices[i]);
sell2 = max(sell2,buy2 + prices[i]);
}
return sell2;
}
};

best-time-to-buy-and-sell-stock-iii leetcode C++的更多相关文章

  1. 123. Best Time to Buy and Sell Stock III ——LeetCode

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

  2. Best Time to Buy and Sell Stock III [LeetCode]

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

  3. Best Time to Buy and Sell Stock III leetcode java

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

  4. 27. Best Time to Buy and Sell Stock && Best Time to Buy and Sell Stock II && Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock (onlineJudge: https://oj.leetcode.com/problems/best-time-to-buy-and- ...

  5. LeetCode 笔记23 Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  6. Best Time to Buy and Sell Stock | & || & III

    Best Time to Buy and Sell Stock I Say you have an array for which the ith element is the price of a ...

  7. 【leetcode】Best Time to Buy and Sell Stock III

    Best Time to Buy and Sell Stock III Say you have an array for which the ith element is the price of ...

  8. LeerCode 123 Best Time to Buy and Sell Stock III之O(n)解法

    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】123. Best Time to Buy and Sell Stock III

    @requires_authorization @author johnsondu @create_time 2015.7.22 19:04 @url [Best Time to Buy and Se ...

  10. LeetCode: Best Time to Buy and Sell Stock III 解题报告

    Best Time to Buy and Sell Stock IIIQuestion SolutionSay you have an array for which the ith element ...

随机推荐

  1. mysql触发器实时检测一条语句进行备份删除

    问题描述:用户有一个这样一个需求,在一张表里会不时出现 "违规" 字样的字段,需要在出现这个字段的时候,把整行的数据删掉.这是个采集任务,如果发现有"违规"字样 ...

  2. TP5路由的位置导致错误

    // 测试 '[js]' => [ ':id' => ['test/test/js', ['method' => 'get'], ['id' => '\d+']], ':id/ ...

  3. k8s garbage collector分析(2)-处理逻辑分析

    garbage collector介绍 Kubernetes garbage collector即垃圾收集器,存在于kube-controller-manger中,它负责回收kubernetes中的资 ...

  4. 这个 MySQL bug 让我大开眼界

    这周收到一个 sentry 报警,如下 SQL 查询超时了. select * from order_info where uid = 5837661 order by id asc limit 1 ...

  5. tomcat 跨域的配置

    * 允许所有跨域  E:\apache-tomcat-7.0.81\conf\web.xml  <filter> <filter-name>CorsFilter</fil ...

  6. 关于selenium添加使用代理ip

    最近在爬某个网站,发现这个网站的反爬太厉害了,正常时候的访问有时候都会给你弹出来验证,验证你是不是蜘蛛,而且requests发的请求携带了请求头信息,cookie信息,代理ip,也能识别是爬虫,他应该 ...

  7. 鸿蒙内核源码分析(系统调用篇) | 开发者永远的口头禅 | 百篇博客分析OpenHarmony源码 | v37.03

    百篇博客系列篇.本篇为: v37.xx 鸿蒙内核源码分析(系统调用篇) | 开发者永远的口头禅 | 51.c.h .o 任务管理相关篇为: v03.xx 鸿蒙内核源码分析(时钟任务篇) | 触发调度谁 ...

  8. 【月光宝盒get√】用时间置换空间,聊聊稀疏数组的那些事儿

    背景 数据结构是指带有结构特性的数据元素的集合.在数据结构中,数据之间通过一定的组织结构关联在一起,便于计算机存储和使用.从大类划分,数据结构可以分为线性结构和非线性结构,适用于不同的应用场景. 线性 ...

  9. Python下载课件

    from urllib.request import urlretrieve # #下载网络文件到本地 import os os.chdir("C:/Users/RankFan/Deskto ...

  10. Conversion Tools(转换工具)

    转换工具 1.Excel # Process: Excel 转表 arcpy.ExcelToTable_conversion("", 输出表, "") # Pr ...