当前位置:网站首页>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]);
}
}边栏推荐
- 2022 2nd cyber edge cup cyber security competition Web
- Lvs+kept highly available cluster
- First knowledge of esp8266 (I) -- access point and wireless terminal mode
- 一种比读写锁更快的锁,还不赶紧认识一下
- [question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
- [numerical analysis exercise] numerical integration (complex trapezoid, complex Simpson, Romberg integral) C with STL implementation
- How to customize logging of.Net6.0
- Mysql 数据恢复流程 基于binlog redolog undolog
- Under the epidemic, the mobile phone supply chain and offline channels are blocked! Sales plummeted and inventory was serious!
- 每条你收藏的资讯背后,都离不开TA
猜你喜欢
![[Marine Science] climate indices data set](/img/0f/f63b946b80fc2cf1d39a975c318abc.png)
[Marine Science] climate indices data set

Common shortcut keys and setting methods of idea

Log4j 漏洞仍普遍存在,并持续造成影响

@Component可以和@Bean 用在同一个类上吗?

Project analysis (from technology to project and product)

Matlab draws the statistical rose chart of wind speed and direction

华能福建公司与华为数字能源深化战略合作,共建低碳智能县域

Interview question: talk about your understanding of AQS

Log4j vulnerability is still widespread and continues to cause impact

Why do server programs need to listen first
随机推荐
Regular expression exercise
The gratitude and resentment between the four swordsmen and code review: "abandon all chaos" to "prodigal son returns"
C language output teaching calendar
QT take out the input box string, lineedit
【StoneDB故障诊断】MDL锁等待
Huaneng Fujian company and Huawei digital energy deepen strategic cooperation and jointly build a low-carbon smart county
Implementation of arbitrary code execution based on.Net dynamic compilation technology
数组扩容、排序、嵌套语句应用
Starrocks community structure comes out, waiting for you to upgrade!
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)
Leetcode 301. delete invalid parentheses
Talk about MySQL transaction two-phase commit
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
It's too voluminous. A company has completely opened its core system (smart system) that has been operating for many years
Wechat applet live broadcast plug-in -- get temporary files (background integrated applet live broadcast)
MySQL data recovery process is based on binlog redolog undo
What is modcount in the source code? What's the effect
Common shortcut keys and setting methods of idea
Inertial navigation principle (VII) -imu error classification (II) -allan variance analysis method +imu test + calibration introduction
Excalidraw: an easy-to-use online, free "hand drawn" virtual whiteboard tool