当前位置:网站首页>LeetCode188. The best time to buy and sell stocks IV
LeetCode188. The best time to buy and sell stocks IV
2022-06-28 21:04:00 【Yuyy】
This paper is finally updated at 484 Days ago, , The information may have developed or changed .
One 、 Ideas
There is an upper limit to choice ,10 A lottery ticket for you to buy , The best you can buy is 10 Zhang . Why care about the number of choices ? Because I have chosen the unlimited number of tradable times in the title : Select the number of transactions , Choose the number of two trades . Then choose the best choice . The essence of dynamic programming is to find out the options , Then make a choice
Two 、 problem
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
Related Topics
- Dynamic programming
\n
- 449
- 0
3、 ... and 、 Code
public int maxProfit(int k, int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
if (k >= length / 2) {
return maxProfit(prices);
}
int[][][] dp = new int[length][k + 1][2];
for (int i = 1; i <= k; i++) {
dp[0][i][0] = 0;
dp[0][i][1] = -prices[0];
}
for (int i = 1; i < length; i++) {
for (int j = k; j > 0; j--) {
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
}
}
return dp[length - 1][k][0];
}
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[length - 1][0];
}Post Views: 320
边栏推荐
猜你喜欢

Application practice | 1billion data second level correlation. Huolala's OLAP System Evolution Based on Apache Doris (with PPT download)

Bitbucket 使用 SSH 拉取仓库失败的问题

Alibaba cloud MSE full link grayscale solution practice based on Apache apisik

什么是接口?什么是接口测试?

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

RT thread thread synchronization and thread communication

Flatten of cnn-lstm

Leetcode 36. 有效的数独(可以,一次过)

ThreadLocal原理

Ehcache配置资料,方便自己查
随机推荐
2. integrate filter
如何使用 DataAnt 监控 Apache APISIX
Embedded dynamic Arabic string conversion LCD display string [thanks for Jianguo ambition]
Leetcode 36. Effective Sudoku (yes, once)
How to do a good job in customer's successful bottom design | tob Master Course
ANR无响应介绍
券商公司开户哪个最靠谱最安全呢
ID access card copied to mobile phone_ How to turn a mobile phone into an access card mobile NFC copy access card graphic tutorial
API 网关 Apache APISIX 助力雪球双活架构演进
ref属性,props配置,mixin混入,插件,scoped样式
Can you make money by speculating in stocks? It's safe to open an account
RT-Thread线程同步与线程通信
如何添加 logs来debug ANR 问题
不同框架的绘制神经网络结构可视化
Keyword long
LeetCode每日一题——剑指 Offer II 091. 粉刷房子
题解 Pie(POJ3122)超详细易懂的二分入门
RT thread thread synchronization and thread communication
Flask - Summary
Which is the most reliable and safe for a securities company to open an account