best-time-to-buy-and-sell-stock-iii leetcode C++
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++的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 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- ...
- 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 ...
- 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 ...
- 【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 ...
- 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 ...
- 【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 ...
- 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 ...
随机推荐
- eclipse安装配置
安装eclipse,并运行了第一个Hello World!
- TypeScript与面向对象
目录 1.引 2.类(class) 3.构造函数和this 4.继承 5.super 6.抽象类 7.接口 8.属性的封装 9.泛型 1.引 简而言之就是程序之中所有的操作都需要通过对象来完成.一切操 ...
- STM32L0系列EEPROM中结构体的读取
在STM32L0中操作EEPROM本来参考了上篇操作FLASH的方法,多多少少都有些问题.我觉得可能是结构体在转换成其他变量的时候出了问题. 比如下面这段代码,在Windows上可以正常运行(使用g+ ...
- centos8安装php扩展memcached报错
在php扩展memcached中执行./configure --with-php-config=/usr/local/php/bin/php-config 报错: error: memcached s ...
- PHP出现iconv(): Detected an illegal character in input string
PHP传给JS字符串用ecsape转换加到url里,又用PHP接收,再用网上找的unscape函数转换一下,这样得到的字符串是UTF-8的,但我需要的是GB2312,于是用iconv转换 开始是这样用 ...
- Java基础系列(36)- 数组三种初始化及内存分析
内存分析 数组三种初始化 静态初始化 int[] a = {1,2,3}; Man[] mans = {new Man(1,1),new Man(2,2)} 动态初始化 int[] a = new i ...
- Linux系列(14) - grep
简述 grep是在文件当中匹配符合条件的字符串,作用是查找文件内容 格式 grep [选项] 字符串 文件名 选项 -i:忽略大小写 -v:排除指定字符串 -n:显示行号 例子 grep " ...
- LeetCode2-链表两数和
目录 LeetCode2-链表两数和 题目描述 示例提示 经验教训 参考正解 题目描述 示例提示 经验教训 链表题的判空条件不是万能的,有时候示例会极其复杂,根本难以通过判空来区分不同情况. /** ...
- Oracle基本入门
一.数据的存储 1.java 程序中的对象:数组.集合保存.当运行的程序结束的时候,里面的数据就消亡. 2.文件存储系统: 存在的缺陷: 2.1)没有明确的数据类型划分. 2.2)没有用户身份验证机制 ...
- 定要过python二级选择题二套
1. 2. 3. 4. 5. 6. 7. 8. 非主属性???? 9. 10.. 11.` 调用只是调用,但是没有返回值,,应为里面没有输出 12. 13. 14. 15. 16. 18. 19.