当前位置:网站首页>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
边栏推荐
- 3. integrate listener
- 不同框架的绘制神经网络结构可视化
- 题解 The SetStack Computer(UVa12096)紫书P116STL的综合应用
- LeetCode560. 和为K的子数组
- Leetcode daily question - 515 Find the maximum value in each tree row
- [learning notes] factor analysis
- ref属性,props配置,mixin混入,插件,scoped样式
- Automatic operation and maintenance platform based on Apache APIs
- 学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
- CNN-LSTM的flatten
猜你喜欢

接口用例设计

【Try to Hack】Cobalt Strike(一)

pyechart绘制多条y轴折线图

How to use dataant to monitor Apache apisex

Ehcache configuration data, convenient for self checking

The principle and source code analysis of Lucene index construction

mysql-发生系统错误1067

CNN-LSTM的flatten

图神经网络也能用作CV骨干模型,华为诺亚ViG架构媲美CNN、Transformer

APISIX 助力中东社交软件,实现本地化部署
随机推荐
Anr analysis - question 1
不同框架的绘制神经网络结构可视化
Understand the construction of the entire network model
基于 Apache APISIX 的自动化运维平台
【笔记:模拟MOS集成电路】带隙基准(基本原理+电流模+电压模电路详解)
CNN-LSTM的flatten
Bitbucket failed to pull the warehouse Using SSH
GlobalSign的泛域名SSL证书
1. integrate servlets
Explanation of memory dump triggered by software watchdog and anr
学习太极创客 — MQTT 第二章(七)ESP8266 MQTT 遗嘱应用
接口用例设计
How to recover after Oracle delete accidentally deletes table data
T检验(检验两个总体的均值差异是否显著)
Comparisonchain file name sort
Data standardization processing
Ehcache配置资料,方便自己查
ANR分析--问题1
题解 The Blocks Problem(UVa101)紫书P110vector的应用
Flask——总结