当前位置:网站首页>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];
}
}
边栏推荐
- 一文搞定JVM常见工具和优化策略
- VOT toolkit environment configuration and use
- 如何快速理解复杂业务,系统思考问题?
- The difference between MVVM and MVC
- Openresty ngx Lua regular expression
- Element operation and element waiting in Web Automation
- Nangou Gili hard Kai font TTF Download with installation tutorial
- Methods modified by static
- 分布式解决方案之TCC
- Boring boring
猜你喜欢
Yiwen gets rid of the garbage collector
Activate function and its gradient
Usage Summary of scriptable object in unity
audiopolicy
Three.js-01 入门
Google Maps case
Element positioning of Web Automation
Matlab smooth curve connection scatter diagram
Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
随机推荐
Tensor attribute statistics
Marginal probability and conditional probability
I closed the open source project alinesno cloud service
Record several frequently asked questions (202207)
Commonly used probability distributions: Bernoulli distribution, binomial distribution, polynomial distribution, Gaussian distribution, exponential distribution, Laplace distribution and Dirac delta d
Common model making instructions
Fix the memory structure of JVM in one article
SPSS analysis of employment problems of college graduates
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Global and Chinese market of networked refrigerators 2022-2028: Research Report on technology, participants, trends, market size and share
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Composition of interface
第十七周作业
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
【Note17】PECI(Platform Environment Control Interface)
Negative sampling
Expectation, variance and covariance
Arduino 测量交流电流
Roman numeral to integer
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]