Leetcode 309 Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:域名whois历史查询 编辑:程序博客网 时间:2024/06/16 07:34
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)
莫名想到了状态机。
整个过程在sell 和 buy之间转换,他们之间的关系如下:
最后一定是sell结尾,所以返回的是s0
public class Solution { public int maxProfit(int[] prices) { if(prices == null || prices.length == 0){ return 0; } int b0 = - prices[0]; int b1 = b0; int s0 = 0; int s1 = 0; int s2 = 0; for(int i = 1; i < prices.length; i++){ b0 = Math.max(b1, s2 - prices[i]); s0 = Math.max(s1, b1 +prices[i]); b1 = b0; s2 = s1; s1 = s0; } return s0; }}
阅读全文
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
- JavaWeb servlet 接收ajax数组
- vector常用操作
- Unable to partial resync with the slave for lack of backlog (Slave request was: 2595405802583).
- 欢迎使用CSDN-markdown编辑器
- test
- Leetcode 309 Best Time to Buy and Sell Stock with Cooldown
- 【Spark的那些事儿】论RDD算子的重要性
- 从高考到程序员之我的感悟
- ural 1003. Parity(并查集)
- 【Bootstrap】响应式原理和设计
- 动态规划算法——弗洛伊德算法(Dynamic Programming Algorithm
- 获取用户当前周期(1=》月经期,2=》排卵期,3=》安全期)
- 机器学习-周志华-个人练习13.4
- BCD码、十六进制与十进制互转