当前位置:网站首页>The optimal time to buy and sell stocks includes the freezing period (leetcode-309)
The optimal time to buy and sell stocks includes the freezing period (leetcode-309)
2022-07-24 09:54:00 【Casten-Wang】
The best time to buy and sell stocks includes a freeze period (LeetCode-309)
subject
Given an array of integers prices, Among them the first prices[i] It means the first one 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 ):
- After sale of shares , You can't buy shares the next day ( That is, the freezing period is 1 God ).
** Be careful :** You can't participate in multiple transactions at the same time ( You have to sell the stock before you buy it again ).
Example 1:
Input : prices = [1,2,3,0,2]
Output : 3
explain : The corresponding transaction status is : [ purchase , sell , Freezing period , purchase , sell ]
Example 2:
Input : prices = [1]
Output : 0
Tips :
1 <= prices.length <= 50000 <= prices[i] <= 1000
Ideas
Five steps
dp[i][j]The meaning of- Four kinds of state
- State one : Buy stock status ( Buying stocks today or before has no operation )
- State two : Sold the stock two days ago , Passed the freezing period
- State three : Sell shares today
- State four : Today is the freezing period
- Four kinds of state
- The recursive formula
- d p [ i ] [ 0 ] dp[i][0] dp[i][0] Can be derived from two states
- The first i − 1 i-1 i−1 Days holding shares , It is equal to d p [ i − 1 ] [ 0 ] dp[i-1][0] dp[i−1][0]
- The first i i i Days to buy shares ( Buy today ), There are two situations
- The day before was frozen : d p [ i − 1 ] [ 3 ] − p r i c e s [ i ] dp[i-1][3]-prices[i] dp[i−1][3]−prices[i]
- The previous day was to keep selling stocks : d p [ i − 1 ] [ 1 ] − p r i c e s [ i ] dp[i-1][1]-prices[i] dp[i−1][1]−prices[i]
- Choose the one with the most cash , That is, the greater of the two
- d p [ i ] [ 1 ] dp[i][1] dp[i][1] Can be derived from two states
- The first i − 1 i-1 i−1 Day is already state two , It is equal to d p [ i − 1 ] [ 1 ] dp[i-1][1] dp[i−1][1]
- The day before was frozen , It is equal to d p [ i − 1 ] [ 3 ] dp[i-1][3] dp[i−1][3]
- Choose the one with the most cash , That is, the greater of the two
- d p [ i ] [ 2 ] dp[i][2] dp[i][2] From only one state
- It must have been a state of buying stocks the day before ( State one ): d p [ i − 1 ] [ 0 ] + p r i c e s [ i ] dp[i-1][0]+prices[i] dp[i−1][0]+prices[i]
- d p [ i ] [ 3 ] dp[i][3] dp[i][3] From only one state
- It must have been the state of selling stocks the day before ( State three ): d p [ i − 1 ] [ 2 ] dp[i-1][2] dp[i−1][2]
- d p [ i ] [ 0 ] dp[i][0] dp[i][0] Can be derived from two states
- Array initialization
dp[0][0]It means the first one 0 Days to buy shares , So it's equal to − p r i c e [ 0 ] -price[0] −price[0]
- traversal order
- Before and after
Code display
class Solution
{
public:
int maxProfit(vector<int> &prices)
{
int n = prices.size();
vector<vector<int>> dp(n, vector<int>(4));
dp[0][0] = -prices[0];
for (int i = 1; i < n; i++)
{
dp[i][0] = max(dp[i - 1][0], max(dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i]));
dp[i][1] = max(dp[i - 1][1], dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + prices[i];
dp[i][3] = dp[i - 1][2];
}
return max(dp[n - 1][1], max(dp[n - 1][2], dp[n - 1][3]));
}
};
This question is very convoluted , I can't write without looking at the solution .
边栏推荐
- [MySQL] - deep understanding of index
- [STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
- PHP Basics - PHP magic constants
- PHP caching system - PHP uses Memcache
- Spark Learning: how to choose different association forms and mechanisms?
- How does SRE and development of Google cooperate
- 详解LinkedList
- System a uses window.open to call system B, and system B carries data back to system a after processing the business
- Compilation and linking of programs
- Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?
猜你喜欢

note: expected ‘void * (***)(void ***)’ but argument is of type ‘void (*)(void *)’
![[don't bother to strengthen learning] video notes (IV) 2. Dqn realizes maze walking](/img/53/5252c7c6989d142cc2ad6b1c6f513c.png)
[don't bother to strengthen learning] video notes (IV) 2. Dqn realizes maze walking
![[don't bother to strengthen learning] video notes (IV) 1. What is dqn?](/img/74/51219a19595f93e7a85449f54d354d.png)
[don't bother to strengthen learning] video notes (IV) 1. What is dqn?

error: field ‘XXX’ declared as a function
![[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus](/img/8f/19b0eb959d2b3f896c8e99f8e673d1.png)
[STM32 learning] (16) STM32 realizes LCD1602 display (74HC595 drive) - 4-bit bus

Lung CT segmentation challenge 2017 dataset download and description
![[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)](/img/6e/b7cf7a8e3296e29d61a0626e3793ea.png)
[STM32 learning] (13) STM32 realizes ultrasonic ranging (hc-sr04)
![Cyclicbarrier and countdownlatch [concurrent programming]](/img/38/3305a0cdb6de40e1370cc93c8e5014.png)
Cyclicbarrier and countdownlatch [concurrent programming]
![[note] what is kernel / user space? Let's start with how the CPU runs the program](/img/b5/0ab4f2841faf3573b4502d2cd09069.png)
[note] what is kernel / user space? Let's start with how the CPU runs the program

OPENCV学习DAY5
随机推荐
[don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?
[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)
cannot unpack non-iterable NoneType object
Selnium checks three conditions when it cannot locate an element
Calculate CPU utilization [Prometheus]
PHP Basics - PHP magic method
Anti shake and throttling
MySQL query database capacity size
Learn more about the synchronized lock upgrade process [concurrent programming]
Raspberry Pie: /bin/sh: 1: bison: not found
[STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack
When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
[don't bother to strengthen learning] video notes (II) 2. Write a small example of Q learning
Arduino- use millis() to do two (or more) things at the same time
MySQL基础篇(一)-- SQL基础
Is it safe for Oriental Fortune futures to open an online account, and will it be cheated?
Spark Learning: using RDD API to implement inverted index
Arduino drive Lora module node
Getting started with identityserver4