Array - 309. Best Time to Buy and Sell Stock with Cooldown
时间:2022-07-25
本文章向大家介绍Array - 309. Best Time to Buy and Sell Stock with Cooldown,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
- Best Time to Buy and Sell Stock with Cooldown
Say you have an array for which the i
th 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:
Input: [1,2,3,0,2] Output: 3 Explanation: transactions = [buy, sell, cooldown, buy, sell]
视频讲解: https://www.bilibili.com/video/bv1bT4y1g76h
思路:
继续是买卖股票的变形题,这一题的多加了一个条件,就是卖完股票得要等一天(cooldown)才可以继续买股票。采用动态规划求解。
- 定义两个状态
hold[i]
和unhold[i]
分别表示第i天hold股票的最大盈利和不hold股票的最大盈利。 - 目标是unhold[n-1] (n为交易天数)
- base case
hold[0] = -prices[0]
、hold[1] = max(-prices[1], -prices[0])
、unhold[0] = 0
。 - 状态转移
hold[i]取最大值:
1、第i天买入
unhold[i-2] - prices[i]
2、第i天没有买入hold[i-1]
unhold[i]取最大值: 1、第i天卖出hold[i-1] + prices[i]
2、第i天没有卖出unhold[i-1]
代码:
java:
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) return 0;
int len = prices.length;
int[] hold = new int[len];
int[] unhold = new int[len];
hold[0] = -prices[0];
for (int i = 1; i < len; i++) {
if (i == 1) {
hold[i] = Math.max(hold[i-1], -prices[i]);
} else {
hold[i] = Math.max(hold[i-1], unhold[i-2] - prices[i]);
}
unhold[i] = Math.max(unhold[i-1], hold[i-1] + prices[i]);
}
return unhold[len-1];
}
}
go:
func maxProfit(prices []int) int {
if prices == nil || len(prices) == 0 {
return 0
}
var hold = make([]int, len(prices))
var unhold = make([]int, len(prices))
hold[0] = -prices[0]
for i := 1; i < len(prices); i++ {
if i == 1 {
hold[i] = max(-prices[1], hold[0])
} else {
hold[i] = max(unhold[i-2] - prices[i], hold[i-1])
}
unhold[i] = max(hold[i-1] + prices[i], unhold[i-1])
}
return unhold[len(prices) - 1]
}
func max(i, j int) int {
if i > j {
return i
}
return j
}
- Mac下nvm管理node.js版本问题
- 自定义tab吸顶效果一(原理)
- OpenGL ES学习001---绘制三角形
- Android之MaterialDesign应用技术2-仿支付宝上滑搜索框缓慢消失
- 披着狼皮的羊——寻找惠普多款打印机中的RCE漏洞
- Android之MaterialDesign应用技术
- 蛙啊!老母亲给你整理了DEDECMS漏洞集合,快回家!
- Android之Bmob移动后端云服务器
- golang基于redis lua封装的优先级去重队列
- Java设计模式总汇二(小白也要飞)
- Java设计模式总汇一
- Android数据绑定技术二,企业级开发
- Android数据绑定技术一,企业级开发
- 工具| sqlmap payload修改之路(下)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 使用 Task 简化异步编程
- Task 编程中的异常处理
- AngularJS 中的 factory、 service 和 provider
- 设计模式之适配器模式
- 设计模式之组合模式
- MvvmCross 框架中的数据绑定语法
- Vivado IP核锁定的解除方法
- Flutter Dojo设计之道——骚气的闪屏动画是如何实现的
- 数据处理 | pandas入门专题——离散化与one-hot
- 聊聊dubbo-go的failfastCluster
- 一分钟学Python|Python的循环语句
- OpenCV4.4 中SIFT特征匹配调用演示
- 这些Python库虽然冷门,但功能真的很强大!
- SQL查找是否"存在",别再count了,很耗费时间的
- 采用 Vue 编写的功能强大的 Swagger-ui 页面