LeetCode Best Time to Buy and Sell Stock with Cooldown(动态规划)
问题:给定一个整数数组,其中第i个元素表示第i天的股票价格。可以多次买卖股票,卖出股票后,无法在第二天买入股票。
思路:动态规划法,用dp(i,j)表示第i天持有或者不持有股票的最大利润。用dp(i,0)表示不持有,dp(i,1)表示持有。状态转移方程为
dp(i,0) = max{dp(i-1,0) , dp(i-1,1) + profit(i)}
dp(i,1)=max{dp(i-1,1), dp(i-2, 0)-profit(i)}
具体代码参考:
https://github.com/wuli2496/OJ/tree/master/LeetCode/Best%20Time%20to%20Buy%20and%20Sell%20Stock%20with%20Cooldown
LeetCode Best Time to Buy and Sell Stock with Cooldown(动态规划)相关推荐
- [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- 【DP + 卖股票】LeetCode 309. Best Time to Buy and Sell Stock with Cooldown
LeetCode 309. Best Time to Buy and Sell Stock with Cooldown Solution1: 比较有难度的一道动态规划题了! 参考网址:http://z ...
- 【LeetCode】309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA)
[LeetCode]309. Best Time to Buy and Sell Stock with Cooldown 最佳买卖股票时机含冷冻期(Medium)(JAVA) 题目地址: https: ...
- [刷题]leetcode #309 - Best Time to Buy and Sell Stock with Cooldown
题目 代码 class Solution {public int maxProfit(int[] prices) {if (prices.length <= 1) return 0;int l ...
- 【LeetCode】解题309:Best Time to Buy and Sell Stock with Cooldown(动态规划)
LeetCode解题 309:Best Time to Buy and Sell Stock with Cooldown (动态规划) Problem 309: Best Time to Buy an ...
- leetcode: Best Time to Buy and Sell Stock 系列
leetcode: Best Time to Buy and Sell Stock 系列 一系列包括: - Best Time to Buy and Sell Stock Ⅰ - Best Time ...
- Leetcode Best Time to Buy and Sell Stock III
Leetcode Best Time to Buy and Sell Stock III,本算法的关键为找出其动态子结构.可以发现,序列中的最小值可以做为其的一个分割,令左边序列为left,右边的序列 ...
- Leetcode Best Time to Buy and Sell Stock
Leetcode Best Time to Buy and Sell Stock 相关代码,本题使用dp算法完成,本算应该算得上一个经典的dp算法题. #include <iostream> ...
- 309. Best Time to Buy and Sell Stock with Cooldown**
309. Best Time to Buy and Sell Stock with Cooldown** https://leetcode.com/problems/best-time-to-buy- ...
最新文章
- FMS3系列学习网上教程
- 文化氛围对新人培养新人的影响
- fedora 15中使用evolution获取exchange邮件
- 一步步编写操作系统 76 用汇编语言编写字符打印函数
- 整合 centos安装python的介绍
- [linux]Error: failure: repodata/repomd.xml from fedora: [Errno 256] No more mirrors to try.
- java自由布局_java怎么在界面上随意摆放位置?
- 一分钟教你学会配置eslint,还在为风格单双引号,对象末位逗号要不要,引用各种报错而烦恼?(建议收藏!详细中文注释)...
- iOS 很酷的动画效果
- 计算长方体和四棱锥的表面积和体积(类的继承)
- JGIT使用的常见问题
- 微信公共平台开发(一):服务器配置
- atomic原子类实现机制_atomic实现原理
- ThinkPHP 的 Vender的简单实用
- 微信小程序开发05 研发加速:使用 Webpack 提升小程序研发效率
- vb.net(C#)调用CAD并画图
- 超过心跳间隔的客户端重连接导致额外的License消耗
- python画好看的图案-妈耶,python的散点图竟然能画这么好看!
- 【TCP/IP学习笔记1】 C语言讲解
- 全球5G发展洞察2022(中)
热门文章
- stm32 systick分析
- TCP/IP UDP用户数据报协议 运输层
- python分析excel数据-对照Excel使用Python进行数据分析,更快掌握
- 精通python-助你精通python,这些学习资源请收好
- 用python画简单的四叶草-使用 python 操作 redis
- python做小程序-【实战案例】利用Python做出趣味版防沉迷小程序
- python创建csv文件并写入-【已解决】Python中创建和保存数据到csv文件中
- python最新版本-官方宣布不再维护Python2,并每年发布一个新版本
- 零基础学python需要多久-零基础学习Python开发需要多长时间?
- python爬虫招聘-Python爬虫实战-抓取boss直聘招聘信息