当前位置:网站首页>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];
}
};
边栏推荐
- cannot unpack non-iterable NoneType object
- [don't bother to strengthen learning] video notes (IV) 2. Dqn realizes maze walking
- What is the cloud native mid platform business architecture?
- Can the "self-help master" who has survived the economic crisis twice continue to laugh this time?
- 《动手学深度学习》(七) -- 边界框和锚框
- [C language] implementation of three versions of address book small project (including source code)
- IdentityServer4入门
- Is CITIC Securities a safe and reliable securities firm? How to open an account?
- It is reported that the prices of some Intel FPGA chip products have increased by up to 20%
- Arduino- use millis() to do two (or more) things at the same time
猜你喜欢

Common evaluation indexes of medical image segmentation

Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)

Dark king | analysis of zego low illumination image enhancement technology

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

【笔记】什么是内核/用户空间 从CPU如何运行程序讲起
![[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?

What is the cloud native mid platform business architecture?
![[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display](/img/f2/6fcd4b2e747b4ceb52a52eda0c1af4.png)
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display

云原生(十二) | Kubernetes篇之Kubernetes基础入门

Spark Learning: compile spark source code in win10
随机推荐
Anti shake and throttling
Spark Learning: how to choose different association forms and mechanisms?
Reading makes people improve my list
程序的编译与链接
Spark Learning: build SQL to meet the specified optimization rules
What happens from the input URL to the page load
C # +opencvsharp+wpf learning notes (I)
[MySQL] - deep understanding of index
LiteOS_ a - SYS_ The run() function is missing a header file.
获取所有股票历史行情数据
JS 84*148=b6a8 how many decimal places can you make both sides equal
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
unity中物体z旋转同步面板上的数值
[example] v-contextmenu right click menu component
Dark king | analysis of zego low illumination image enhancement technology
Source insight 3.5 comment garbled
[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?
Firewalld firewall related commands
[don't bother with reinforcement learning] video notes (I) 2. Summary of reinforcement learning methods
PHP Basics - PHP magic constants