当前位置:网站首页>Leecode learning notes
Leecode learning notes
2022-07-05 22:59:00 【Engineering students who like history】
- Stock issues - You can only buy it once
Because you can only buy it once , So the definition of dp[i][0] The first i Days holding shares , Can only be -price[i], There can be no accumulation of profits , namely
dp[i - 1][1]-prices[i]
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][2];
dp[0][0] = -prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], - prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[prices.length - 1][1];
}
}
- Stock issues - You can buy many times
You can buy many times here , therefore dp[i][0] Holding the profits of holding stocks every day can consider the previous profits , namely
dp[i - 1][1] - prices[i]
( There was no profit from holding shares the previous day - The stock price of the day , This indicates that the previous transaction has ended , The rest is the profit from not holding shares the day before )
class Solution {
public int maxProfit(int[] prices) {
// int res = 0;
// for (int i = 1; i < prices.length; i++) {
// res += Math.max(prices[i] - prices[i - 1], 0);// Only collect positive profits
// }
// return res;
int[][] dp = new int[prices.length][2];
dp[0][0] = - prices[0];
dp[0][1] = 0;
for (int i = 1; i < prices.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[prices.length - 1][1];
}
}
- Stock issues - Bought it twice
I bought this twice , In fact, it is similar to the first stock problem , You can't buy or sell many times , Just like the recurrence of the first case , Just add the status of the second purchase
class Solution {
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][4];
dp[0][0] = -prices[0];
dp[0][2] = -prices[0];
for (int i = 1; i < prices.length; i++) {
// The first i The profit of holding shares for the first time
dp[i][0] = Math.max(dp[i - 1][0], - prices[i]);
// The first i For the first time, the profit of not holding shares
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
// The first i The profit of holding shares for the second time in the day
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] - prices[i]);
// The first i For the second time in the day, the profit of not holding shares
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] + prices[i]);
}
return dp[prices.length - 1][3];
}
}
边栏推荐
- Event trigger requirements of the function called by the event trigger
- PLC编程基础之数据类型、变量声明、全局变量和I/O映射(CODESYS篇 )
- [speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
- 【无标题】
- audiopolicy
- LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
- CJ mccullem autograph: to dear Portland
- Overview of Fourier analysis
- I closed the open source project alinesno cloud service
- My experience and summary of the new Zhongtai model
猜你喜欢
一文搞定JVM常见工具和优化策略
Usage Summary of scriptable object in unity
Douban scoring applet Part-2
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
Vision Transformer (ViT)
一文搞定class的微觀結構和指令
SPSS analysis of employment problems of college graduates
TypeError: this. getOptions is not a function
实现反向代理客户端IP透传
随机推荐
audiopolicy
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
2022.02.13 - SX10-30. Home raiding II
Ultrasonic sensor flash | LEGO eV3 Teaching
Global and Chinese markets for welding products 2022-2028: Research Report on technology, participants, trends, market size and share
Thoroughly understand JVM class loading subsystem
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
The difference between MVVM and MVC
我把开源项目alinesno-cloud-service关闭了
Binary tree (III) -- heap sort optimization, top k problem
Ieventsystemhandler event interface
Nacos 的安装与服务的注册
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
[untitled]
First, redis summarizes the installation types
Tiktok__ ac_ signature
Expectation, variance and covariance
二叉树(三)——堆排序优化、TOP K问题