309. Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:知乎 印度自大 编辑:程序博客网 时间:2024/05/16 00:39
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]
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
public class Solution { public int maxProfit(int[] prices) { int sell = 0, prev_sell = 0, buy = Integer.MIN_VALUE, prev_buy; for (int price : prices) { prev_buy = buy; buy = Math.max(prev_sell - price, prev_buy); prev_sell = sell; sell = Math.max(prev_buy + price, prev_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
- PHP文件上传至另一台服务器
- windows提示电脑已经关机了但主机还在运行
- PHP数组格式的所有名族代码和名称
- iframe去掉滚动条
- 三种东西永远不要放到数据库里
- 309. Best Time to Buy and Sell Stock with Cooldown
- 聚来宝的项目怎么样?网赚创业项目介绍
- 聚来宝获商务部《媒体购物国家标准》制订单位 【2012-12-22 17:04:20】【来源:京华网】【字体:大 中 小】【颜色:红 绿 蓝 黑】【分享新闻】
- Android 之 下拉框(Spinner)的简单使用
- 浙江不老仙妻,教去皱
- 为什么某些网页打不开而别人却能打开?
- 数据库连接池实现原理
- iOS开发调试技巧总结(持续更新中)
- mysql_connect()与mysqli_connect()