当前位置:网站首页>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];
}
}
边栏推荐
- 2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
- Roman numeral to integer
- 我把开源项目alinesno-cloud-service关闭了
- Registration and skills of hoisting machinery command examination in 2022
- Expectation, variance and covariance
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- Distributed solution selection
- 一文搞定垃圾回收器
- Ultrasonic sensor flash | LEGO eV3 Teaching
- Thoroughly understand JVM class loading subsystem
猜你喜欢
一文搞定JVM常见工具和优化策略
My experience and summary of the new Zhongtai model
【Note17】PECI(Platform Environment Control Interface)
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
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
TCC of distributed solutions
【无标题】
CJ mccullem autograph: to dear Portland
SPSS analysis of employment problems of college graduates
The method and principle of viewing the last modification time of the web page
随机推荐
Starting from 1.5, build a micro Service Framework -- log tracking traceid
分布式解决方案选型
Record several frequently asked questions (202207)
openresty ngx_lua正則錶達式
终于搞懂什么是动态规划的
Go language learning tutorial (XV)
Simple and beautiful method of PPT color matching
CJ mccullem autograph: to dear Portland
Douban scoring applet Part-2
Unity Max and min constraint adjustment
Vision Transformer (ViT)
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Common JVM tools and optimization strategies
LeetCode102. Sequence traversal of binary tree (output by layer and unified output)
Composition of interface
Arduino measures AC current
Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
The introduction to go language is very simple: String
我对新中台模型的一些经验思考总结