当前位置:网站首页>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];
}
}
边栏推荐
- 基于STM32的ADC采样序列频谱分析
- 我对新中台模型的一些经验思考总结
- Douban scoring applet Part-2
- openresty ngx_lua正则表达式
- The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
- Distributed solution selection
- 【Note17】PECI(Platform Environment Control Interface)
- Global and Chinese markets for children's amusement facilities 2022-2028: Research Report on technology, participants, trends, market size and share
- 查看网页最后修改时间方法以及原理简介
- EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
猜你喜欢

I closed the open source project alinesno cloud service

The method and principle of viewing the last modification time of the web page

Common model making instructions

My experience and summary of the new Zhongtai model

How can easycvr cluster deployment solve the massive video access and concurrency requirements in the project?

Hcip day 12 (BGP black hole, anti ring, configuration)

LeetCode145. Post order traversal of binary tree (three methods of recursion and iteration)

2022.02.13 - SX10-30. Home raiding II

东南亚电商指南,卖家如何布局东南亚市场?

d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL
随机推荐
VOT Toolkit环境配置与使用
Common model making instructions
Nanjing: full use of electronic contracts for commercial housing sales
Function default parameters, function placeholder parameters, function overloading and precautions
Global and Chinese markets of tantalum heat exchangers 2022-2028: Research Report on technology, participants, trends, market size and share
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
二叉树(三)——堆排序优化、TOP K问题
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
[screen recording] how to record in the OBS area
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
The difference between MVVM and MVC
Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
The countdown to the launch of metaverse ape is hot
Alibaba Tianchi SQL training camp task4 learning notes
audiopolicy
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Registration and skills of hoisting machinery command examination in 2022
一文搞定class的微觀結構和指令
Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share