309. Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:会linux能找什么工作 编辑:程序博客网 时间:2024/06/03 21:13
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 = 3transactions = [buy, sell, cooldown, buy, sell]唯一的感想,每次做动态规划的题都觉得我是智障。
buy[i]=max(sell[i-2]-p,buy[i-1])
sell[i]=max(buy[i-1]+p,sell[i-1])
public class Solution { public int maxProfit(int[] prices) { int sell=0,prv_sell=0,buy=Integer.MIN_VALUE,prv_buy; for(int p:prices){ prv_buy=buy; buy=Math.max(prv_sell-p,prv_buy); prv_sell=sell; sell=Math.max(prv_buy+p,prv_sell); } return sell; }}
0 0
- 309.Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 309. Best Time to Buy and Sell Stock with Cooldown
- 循环链表之不敢死队问题(本质是约瑟夫环)
- task_strcut结构体
- POJ2390 Bank Interest【水题】
- 【OpenCV】10th-帧差法
- 别让CDN的回源把你的服务器拖垮,采用正确的回源策略
- 309. Best Time to Buy and Sell Stock with Cooldown
- WildFly配置MySql驱动
- informix的rowid寻址过程分析
- Netty4.x中文教程系列(一)目录及概述
- 数据结构实验之二叉树的建立与遍历
- color
- POJ - 1751----Highways(Kruskal)
- Windows 安装theano
- contextTypes was defined as an instance property on MyButton. Use a static property to define contex