Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
来源:互联网 发布:迪拜购物 知乎 编辑:程序博客网 时间:2024/04/29 19:02
State machine graph
/** * state machine approach * state a(hold): hold stack, sell or rest * state b(sold): just sold stack can only rest * state c(rest): cooldown, buy or rest * translate to formula, * stateA = max(preStateA, preStateC-prices[i]) * stateB = preStateA+prices[i] * stateC = max(preStateB, preStateC) */ public class Solution { public int maxProfit(int[] prices) { if (prices.length < 2) return 0; // initialization int hold = -prices[0]; int sold = Integer.MIN_VALUE; int rest = 0; for (int i=1; i<prices.length; i++) { int preHold = hold; hold = Math.max(hold, rest-prices[i]); rest = Math.max(rest, sold); sold = preHold + prices[i]; } return Math.max(sold, rest); }}
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
- 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
- 机器人相关资源
- 3-纯C++代码编写helloworld
- C++ primer 十三 C++中的类(二)
- 判断
- Cmake 详解
- Leetcode 309. Best Time to Buy and Sell Stock with Cooldown
- OSGI企业应用开发(三)Eclipse中搭建Equinox运行环境
- 《工大 工大》
- Ubuntu安装sublime-text3详细教程
- 怎么在windows下使用gvim
- Centos7安装mongodb出现的问题:服务无法启动
- 统一监控报警平台的架构设计思路分享
- 配置hadoop HIVE元数据保存在mysql中
- jzoj4922 环 [性质、枚举答案、转换模型]