309. Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:永强集团淘宝店是什么 编辑:程序博客网 时间:2024/05/16 06:11
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]Solution
https://leetcode.com/discuss/72030/share-my-dp-solution-by-state-machine-thinking
//https://leetcode.com/discuss/72030/share-my-dp-solution-by-state-machine-thinkingpublic int maxProfit(int[] prices) {if(prices == null || prices.length == 0){return 0;}int[] s0 = new int[prices.length];int[] s1 = new int[prices.length];int[] s2 = new int[prices.length];s0[0] = 0;s1[0] = -prices[0];s2[0] = Integer.MIN_VALUE;for(int i = 1; i < prices.length; i++){s0[i] = Math.max(s0[i - 1], s2[i - 1]);s1[i] = Math.max(s0[i - 1] - prices[i], s1[i - 1]);s2[i] = s1[i - 1] + prices[i];}return Math.max(s0[prices.length - 1], s2[prices.length - 1]);}
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
- 带模板参数的运算符重载
- 在阿里云中编译Linux4.5.0内核 - Ubuntu内核编译教程
- LeetCode 107 Binary Tree Level Order Traversal II
- 译:Google的大规模集群管理工具Borg(一)------ 用户视角的Borg特性
- 用启动U盘安装ubuntu14.04
- 309. Best Time to Buy and Sell Stock with Cooldown
- 232. Implement Queue using Stacks
- java程序控制台编译成功但无法运行,报“找不到或无法加载主类”错误!
- iOS开发的兼容和适配问题
- 既然来都来了,那就把以前写在其他博客上的文章都搬过来吧
- (2012-02-15 旧博文搬家)第一个网站总算是基本完工了
- DEVICE_ATTR 详解
- LeetCode 254. Factor Combinations(因式分解)
- 第二十六课 实现线性表基本操作的函数 【项目1-4】