当前位置:网站首页>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];
}
}
边栏推荐
- 70. Climbing Stairs. Sol
- GWT module may need to be (RE) compiled reduce - GWT module may need to be (RE) compiled reduce
- Binary tree (III) -- heap sort optimization, top k problem
- Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
- Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程
- audiopolicy
- All expansion and collapse of a-tree
- 一文搞定class的微觀結構和指令
- 利用LNMP实现wordpress站点搭建
- VOT Toolkit环境配置与使用
猜你喜欢
![[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]](/img/41/4de83d2c81b9e3485d503758e12108.jpg)
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
![[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]](/img/df/9aa83ac5bd9f614942310a040a6dff.jpg)
[speech processing] speech signal denoising and denoising based on Matlab GUI low-pass filter [including Matlab source code 1708]

VOT toolkit environment configuration and use
![[untitled]](/img/98/aa874a72f33edf416f38cb6e92f654.png)
[untitled]
![[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)](/img/48/cd7960bbbc51a967b20da410bf81fe.jpg)
[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)

鏈錶之雙指針(快慢指針,先後指針,首尾指針)

Yiwen gets rid of the garbage collector

第一讲:蛇形矩阵

Metaverse Ape猿界应邀出席2022·粤港澳大湾区元宇宙和web3.0主题峰会,分享猿界在Web3时代从技术到应用的文明进化历程

How to quickly experience oneos
随机推荐
50. Pow(x, n). O(logN) Sol
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
70. Climbing Stairs. Sol
Codeforces Global Round 19
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
Opencv judgment points are inside and outside the polygon
Roman numeral to integer
Vcomp110.dll download -vcomp110 What if DLL is lost
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
我把开源项目alinesno-cloud-service关闭了
TCC of distributed solutions
My experience and summary of the new Zhongtai model
Metaverse Ape上线倒计时,推荐活动火爆进行
First, redis summarizes the installation types
BFC block level formatting context
Nacos installation and service registration
700. Search in a Binary Search Tree. Sol
The difference between MVVM and MVC
509. Fibonacci Number. Sol