leetcode 309 : Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:电脑淘宝怎么解除绑定 编辑:程序博客网 时间:2024/05/21 06:49
1、原题如下:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)
Example:prices = [1, 2, 3, 0, 2]
maxProfit = 3
transactions = [buy, sell, cooldown, buy, sell]
2、解题如下:
class Solution {public: int maxProfit(vector<int>& prices) { int buy(INT_MIN),sell(0),p_sell(0),p_buy; for(auto price:prices) { p_buy=buy; buy=max(p_sell-price,buy); p_sell=sell; sell=max(p_buy+price,sell); } return sell; }};
0 0
- leetcode Best Time to Buy and Sell Stock with Cooldown
- LeetCode Best Time to Buy and Sell Stock with Cooldown
- Leetcode: Best Time to Buy and Sell Stock with Cooldown
- [LeetCode]Best Time to Buy and Sell Stock with Cooldown
- Leetcode Best Time to Buy and Sell Stock with Cooldown
- leetcode - Best Time to Buy and Sell Stock with Cooldown
- LeetCode:Best Time to Buy and Sell Stock with Cooldown
- leetcode Best Time to Buy and Sell Stock with Cooldown
- Leetcode Best Time to Buy and Sell Stock with Cooldown
- LeetCode:Best Time to Buy and Sell Stock with Cooldown
- LeetCode--Best Time to Buy and Sell Stock with Cooldown
- 309Best Time to Buy and Sell Stock with Cooldown
- [309] Best Time to Buy and Sell Stock with Cooldown
- leetcode 309 : Best Time to Buy and Sell Stock with Cooldown
- 【LEETCODE】309-Best Time to Buy and Sell Stock with Cooldown
- LeetCode 309 Best Time to Buy and Sell Stock with Cooldown
- leetcode——309——Best Time to Buy and Sell Stock with Cooldown
- LeetCode[309] Best Time to Buy and Sell Stock with Cooldown
- JMeter学习——JMeter测试Java
- Java 语言中 Enum 类型的使用介绍
- Form表单数据传输方式之Get与Post区别
- spring学习网站
- \backend\models\core\Help
- leetcode 309 : Best Time to Buy and Sell Stock with Cooldown
- HDFS TFS
- MySQLCluster7.2.15安装部署性能测试
- CSS3旋转球3D视觉效果
- @Repository、@Service、@Controller 和 @Component
- JVM调优的相关设置
- EditText输入长度限制,并提示信息
- 自定义URL Scheme详解
- Enterprise Architect 使用技巧和遇到的疑难问题