当前位置:网站首页>The best time to buy and sell stocks Ⅳ (leetcode-188)
The best time to buy and sell stocks Ⅳ (leetcode-188)
2022-07-24 09:54:00 【Casten-Wang】
The best time to buy and sell stocks Ⅳ(LeetCode-188)
subject
Given an array of integers prices , It's the first i Elements prices[i] Is a given stock in the i Sky price .
Design an algorithm to calculate the maximum profit you can get . You can do at most k transaction .
** 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 :k = 2, prices = [2,4,1]
Output :2
explain : In the 1 God ( Stock price = 2) Buy when , In the 2 God ( Stock price = 4) Sell when , The exchange will make a profit = 4-2 = 2 .
Example 2:
Input :k = 2, prices = [3,2,6,5,0,3]
Output :7
explain : In the 2 God ( Stock price = 2) Buy when , In the 3 God ( Stock price = 6) Sell when , The exchange will make a profit = 6-2 = 4 .
And then , In the 5 God ( Stock price = 0) Buy when , In the 6 God ( Stock price = 3) Sell when , The exchange will make a profit = 3-0 = 3 .
Tips :
0 <= k <= 1000 <= prices.length <= 10000 <= prices[i] <= 1000
Ideas
At the best time to buy and sell stocks Ⅲ(LeetCode-123) after , I must understand, except for the State 0, The others are odd buy , Sell even
I won't write the five series , Direct reference to the best time to buy and sell stocks Ⅲ(LeetCode-123)
Code display
class Solution
{
public:
int maxProfit(int k, vector<int> &prices)
{
int n = prices.size();
if (n == 0)
{
return 0;
}
vector<vector<int>> dp(n, vector<int>(2 * k + 1));
for (int j = 0; j < k; j++)
{
dp[0][j * 2 + 1] = -prices[0];
}
for (int i = 1; i < n; i++)
{
for (int j = 0; j < k; j++)
{
dp[i][j * 2 + 1] = max(dp[i - 1][j * 2] - prices[i], dp[i - 1][j * 2 + 1]);
dp[i][j * 2 + 2] = max(dp[i - 1][j * 2 + 1] + prices[i], dp[i - 1][j * 2 + 2]);
}
}
return dp[n - 1][k * 2];
}
};
边栏推荐
- Arduino drive Lora module node
- Opencv learning Day5
- Embedded development: Tools - optimizing firmware using DRT
- Spark Learning: a form of association in a distributed environment?
- Arduino- use millis() to do two (or more) things at the same time
- Ask you to build a small program server
- Scala learning: why emphasize immutable objects?
- [STM32 learning] (12) STM32 realizes LCD1602 simple static reality
- Is it safe for Oriental Fortune futures to open an online account, and will it be cheated?
- 2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
猜你喜欢
![[example] v-contextmenu right click menu component](/img/d7/9287b24a6d9ada01a7f258dd34f0f8.jpg)
[example] v-contextmenu right click menu component

C#/VB. Net: convert word or EXCEL documents to text
![[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?
![[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)](/img/dd/d29a5be7306580ad388ba6487d230f.png)
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)

It's eleven again. Those jokes about nagging programmers going home for blind dates

When the hot tea is out of stock, what does the new tea drink rely on to continue its life?

Hucang integrated e-commerce project (I): introduction to the project background and structure
![[STM32 learning] (14) two 74HC595 controls four nixie tube displays](/img/22/c83e29bead8e6298a0a3564419022c.png)
[STM32 learning] (14) two 74HC595 controls four nixie tube displays

Spark Learning: Spark implementation of distcp
![[STM32 learning] (6) use of serial port 1 (usart1)](/img/b1/430d3501a99e46958c066f7fd7eee9.png)
[STM32 learning] (6) use of serial port 1 (usart1)
随机推荐
[STM32 learning] (5) press the key to control the flow light (interrupt Implementation)
This article takes you to understand the dynamic memory allocation of C language
JS bind simulation
Firewalld firewall related commands
C#/VB. Net: convert word or EXCEL documents to text
Friends come to interview a unicorn company in Beijing at leisure. The interview question is priced at 25K
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)
Li Kou 300 longest increasing subsequence dynamic programming
2022 trusted cloud authoritative assessment released: Tianyi cloud has obtained ten certifications and five best practices
Where is the bitbucket clone address
Server load and CPU performance tuning
Reading makes people improve my list
Tag the specified commit and submit the tag
Ask you to build a small program server
JS 84*148=b6a8 how many decimal places can you make both sides equal
ASI-20220222-Implicit PendingIntent
Dorissql syntax Usage Summary
Web page opening speed is very slow, how to solve it?
Arduino- how to light the LED?
Cyclicbarrier and countdownlatch [concurrent programming]