当前位置:网站首页>The best time to buy and sell stocks includes handling charges (leetcode-714)
The best time to buy and sell stocks includes handling charges (leetcode-714)
2022-07-24 09:54:00 【Casten-Wang】
The best time to buy and sell stocks includes handling fees (LeetCode-714)
subject
Given an array of integers prices, among prices[i] It means the first one i Day's share price ; Integers fee Represents the handling fee for trading shares .
You can close the deal indefinitely , But you have to pay a handling fee for every transaction . If you have bought a stock , You can't buy any more stock until you sell it .
Return the maximum profit .
** Be careful :** A deal here refers to the whole process of buying, holding and selling stocks , You only need to pay a handling fee for each transaction .
Example 1:
Input :prices = [1, 3, 2, 8, 4, 9], fee = 2
Output :8
explain : The maximum profit that can be achieved :
Buy... Here prices[0] = 1
Sell... Here prices[3] = 8
Buy... Here prices[4] = 4
Sell... Here prices[5] = 9
Total profit : ((8 - 1) - 2) + ((9 - 4) - 2) = 8
Example 2:
Input :prices = [1,3,7,5,10,3], fee = 3
Output :6
Tips :
1 <= prices.length <= 5 * 1041 <= prices[i] < 5 * 1040 <= fee < 5 * 104
Ideas
and LeetCode-122 About the same type , As long as the handling fee is deducted when selling
Five steps
dp[i][ take 0 or 1]The meaning of- d p [ i ] [ 0 ] dp[i][0] dp[i][0] It means the first one i i i Cash from holding the stock for days
- d p [ i ] [ 1 ] dp[i][1] dp[i][1] It means the first one i i i Cash from not holding the stock for days
- 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 , Because you can buy many times , There may be a situation where there has been a round of trading to generate profits before , The cash will be after buying today d p [ i − 1 ] [ 1 ] − p r i c e [ i ] dp[i-1][1]-price[i] dp[i−1][1]−price[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 Days do not hold shares , It is equal to d p [ i − 1 ] [ 1 ] dp[i-1][1] dp[i−1][1]
- The first i i i Days to sell shares , It is equal to p r i c e [ i ] + d p [ i − 1 ] [ 0 ] − f e e price[i]+dp[i-1][0]-fee price[i]+dp[i−1][0]−fee
- Choose the one with the most cash , That is, the greater of the two
- 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 holding shares , So it's equal to − p r i c e [ 0 ] -price[0] −price[0]dp[0][1]It means the first one 0 Days do not hold shares , be equal to 0
- traversal order
- Before and after
Code display
class Solution
{
public:
int maxProfit(vector<int> &prices, int fee)
{
int n = prices.size();
vector<vector<int>> dp(n, vector<int>(2));
dp[0][0] = -prices[0];
dp[0][1] = 0;
for (int i = 1; i < n; i++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
}
return dp[n - 1][1];
}
};
边栏推荐
- Common evaluation indexes of medical image segmentation
- 云原生(十二) | Kubernetes篇之Kubernetes基础入门
- PHP Basics - PHP super global variables
- Arduino- how to light the LED?
- Do you really understand the concept of buffer? Take you to uncover the buffer zone~
- String sort
- Spark Learning: Spark implementation of distcp
- Cess test online line! The first decentralized storage network to provide multiple application scenarios
- Understanding of magnetic parameters in Hall sensors
- Financial digital transformation
猜你喜欢
![[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?](/img/57/0ebff0839d2a2898472d3270fd13df.png)
[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?

Raspberry Pie: serial port login does not display print information
![[STM32 learning] (12) STM32 realizes LCD1602 simple static reality](/img/78/954ebaae0cce5d9387e7032fa85e60.png)
[STM32 learning] (12) STM32 realizes LCD1602 simple static reality

In the envy of LETV, there is a "meaning crisis" of contemporary workers
![[don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?](/img/84/48a6a83192a12dafd88bcd74db0955.gif)
[don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?

Spark Learning: compile spark source code in win10
![[don't bother with intensive learning] video notes (III) 1. What is SARS?](/img/5c/4636db2849ba8514976a5afaf56e38.png)
[don't bother with intensive learning] video notes (III) 1. What is SARS?
![[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

Compilation and linking of programs

Build practical product help documents to improve user satisfaction
随机推荐
Yarn: unable to load file
Raspberry Pie: /bin/sh: 1: bison: not found
The heads of the five major international institutions called for urgent action to deal with the global food security crisis
[STM32 learning] (4) press the key to control the flow light
What are the 6% annualized products?
Dark king | analysis of zego low illumination image enhancement technology
[STM32 learning] (7) use of serial port 2 (usart2)
Lung CT segmentation challenge 2017 dataset download and description
Centos7 install mysql8.0
[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?
Openstack network neutron knowledge point "openstack"
Tag the specified commit and submit the tag
Arduino drive lcd1602a
What is the cloud native mid platform business architecture?
Web page opening speed is very slow, how to solve it?
[STM32 learning] (11) STM32 Mifare_ Use of one (S50) m1s50 (read, write, key modification, control bit interpretation)
Common evaluation indexes of medical image segmentation
Anti shake and throttling
LiteOS_ a - SYS_ The run() function is missing a header file.
Source insight 3.5 comment garbled