当前位置:网站首页>Leetcode-309- best time to buy and sell stocks, including freezing period
Leetcode-309- best time to buy and sell stocks, including freezing period
2022-07-27 22:11:00 【benym】
# LeetCode-309- The best time to buy and sell stocks includes the freezing period
Given an array of integers , Among them the first i Elements represent the i Day's share price .
Design an algorithm to calculate the maximum profit . Under the following constraints , You can do as many deals as you can ( Buy and sell a stock many times ):
You can't participate in multiple transactions at the same time ( You have to sell the stock before you buy it again ). After sale of shares , You can't buy shares the next day ( That is, the freezing period is 1 God ).
Example 1:
Input : [1,2,3,0,2]
Output : 3
explain : The corresponding transaction status is : [ purchase , sell , Freezing period , purchase , sell ]
# Their thinking
For details, see links. https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/solution/yi-ge-fang-fa-tuan-mie-6-dao-gu-piao-wen-ti-by-lab/
# Java Code
class Solution {
public int maxProfit(int[] prices) {
if(prices == null || prices.length == 0){
return 0;
}
// State transition diagram :
// holding Not holding shares
// -----、 sell -----、
// holding -----↑--------→ Not holding shares ---↑
// |
// | sell
// | Out
// ↓
// Freezing period ( Nothing can be done during this period )
//
int[][] dp = new int[prices.length][3];
//dp[i][x] The first i Day in x state (0. Not holding shares ,1. holding ,2. Freezing period )
// Not holding shares
dp[0][0] = 0;
// holding
dp[0][1] = -prices[0];
// Freezing period
dp[0][2] = 0;
for(int i = 1;i < prices.length;i++){
// The first i Tianbu holdings can be transferred from two states ,1. The first i-1 Tianbu shares , I still don't buy stocks today , Keep non shareholding .2. The freezing period is over , But don't buy stocks today .
dp[i][0] = Math.max(dp[i-1][0], dp[i-1][2]);
// The first i Tian shareholding can be transferred from two states ,1. The first i-1 Tianbu shares ( Including whether yesterday was a frozen period or yesterday itself did not hold shares ), Buy stocks today .2. The first i-1 Tian Holdings , Not sold today , Maintain shareholding .
dp[i][1] = Math.max(dp[i - 1][0] - prices[i], dp[i - 1][1]);
// Only the first one i God sold the stock , The first i Genius can enter the freezing period .
dp[i][2] = dp[i-1][1] + prices[i];
}
// Only the last day does not hold shares ( Non shareholding status ) Or it was sold the day before ( Today is the freezing period ) In both cases, money is in hand , The maximum value occurs in both .
return Math.max(dp[prices.length - 1][0], dp[prices.length - 1][2]);
}
}边栏推荐
- Excalidraw:很好用的在线、免费「手绘」虚拟白板工具
- For 3nm and below processes, ASML new generation EUV lithography machine exposure
- 只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)
- Shengyang technology officially launched the remote voiceprint health return visit service system!
- ApacheSpark-命令执行(CVE-2022-33891) 漏洞复现
- V2.X 同步异常,无法云端同步的帖子一大堆,同步又卡又慢
- Mask automatic update description file (mask description file)
- Understanding of L1 regularization and L2 regularization [easy to understand]
- Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
- First knowledge of esp8266 (I) -- access point and wireless terminal mode
猜你喜欢

每条你收藏的资讯背后,都离不开TA

8000 word explanation of OBSA principle and application practice
![[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/dd/5ef46cff3988db57bfaf6fe925a0e0.jpg)
[question 21] idiom Solitaire (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

Microsoft store can't download apps, vs2019 can't download plug-ins solution

【海洋科学】海洋气候指数【Climate Indices】数据集

ThreadLocal principle and source code analysis (click in step by step, don't recite, learn ideas)

B站崩了,如果我们是那晚负责修复的开发人员

The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"

关系型数据库的设计思想,20张图给你看的明明白白

Log4j 漏洞仍普遍存在,并持续造成影响
随机推荐
Log4j 漏洞仍普遍存在,并持续造成影响
8000 word explanation of OBSA principle and application practice
Exception -exception
First zhanrui 5g chip! Exposure of Hisense F50, a pure domestic 5g mobile phone: equipped with Huben T710 + chunteng 510
Interview question: talk about your understanding of AQS
More than 100 lines should be split into functions
Form of objects in memory & memory allocation mechanism
枚举和注解
2021-11-05 understanding of class variables and class methods
7行代码让B站崩溃3小时
Shengyang technology officially launched the remote voiceprint health return visit service system!
Matplotlib 多子图绘制
Huaneng Fujian company and Huawei digital energy deepen strategic cooperation and jointly build a low-carbon smart county
Pytoch distributed training
Cocoapods reload
Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
What is modcount in the source code? What's the effect
华能福建公司与华为数字能源深化战略合作,共建低碳智能县域
Talk about MySQL transaction two-phase commit
直播软件app开发,uniapp scroll-view隐藏滚动条