当前位置:网站首页>leecode-学习笔记
leecode-学习笔记
2022-07-05 22:45:00 【喜欢历史的工科生】
- 股票问题-只能购买一次
因为这里只能购买一次,所以定义的dp[i][0]第i天持有股票,只能是-price[i],不能有利润的累计,即
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];
}
}
- 股票问题-可以购买多次
这里可以购买多次,所以dp[i][0]持有每天持有股票的利润可以考虑之前的利润,即
dp[i - 1][1] - prices[i](前一天没有持有股票的利润-当天的股票价格,这就表明之前的交易已经结束,剩下的是前一天没持有股票的利润)
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);//只搜集正利润
// }
// 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];
}
}
- 股票问题-买了两次
这个买了两次,实际上是和第一种股票问题相似,不可以多次买卖,就要和第一种情况的递推一样,只不过加了第二次购买的状态
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++) {
//第i天第一次持有股票的利润
dp[i][0] = Math.max(dp[i - 1][0], - prices[i]);
//第i天第一次不持有股票的利润
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
//第i天第二次持有股票的利润
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] - prices[i]);
//第i天第二次不持有股票的利润
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] + prices[i]);
}
return dp[prices.length - 1][3];
}
}
边栏推荐
- 二叉树(二)——堆的代码实现
- 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
- Global and Chinese market of water treatment technology 2022-2028: Research Report on technology, participants, trends, market size and share
- 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
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- 谷歌地图案例
- Google Maps case
- a-tree 树的全部展开和收起
- Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
- 【Note17】PECI(Platform Environment Control Interface)
猜你喜欢

How to quickly experience oneos

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

Nacos installation and service registration

Nanjing: full use of electronic contracts for commercial housing sales

【Note17】PECI(Platform Environment Control Interface)

opencv 判断点在多边形内外
![[untitled]](/img/98/aa874a72f33edf416f38cb6e92f654.png)
[untitled]

【无标题】

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

Arduino measures AC current
随机推荐
Vcomp110.dll download -vcomp110 What if DLL is lost
二叉树(三)——堆排序优化、TOP K问题
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?
QT creator 7 beta release
Nail error code Encyclopedia
Ieventsystemhandler event interface
2022.02.13 - SX10-30. Home raiding II
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
点到直线的距离直线的交点及夹角
Record several frequently asked questions (202207)
Overriding equals() & hashCode() in sub classes … considering super fields
Spectrum analysis of ADC sampling sequence based on stm32
Postman core function analysis - parameterization and test report
谷歌地图案例
700. Search in a Binary Search Tree. Sol
Binary tree (II) -- code implementation of heap
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]
SPSS analysis of employment problems of college graduates
Assign the output of a command to a variable [repeat] - assigning the output of a command to a variable [duplicate]