当前位置:网站首页>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];
}
}
边栏推荐
- Global and Chinese markets for reciprocating seal compressors 2022-2028: Research Report on technology, participants, trends, market size and share
- Overview of Fourier analysis
- Paddle Serving v0.9.0 重磅发布多机多卡分布式推理框架
- MoCo: Momentum Contrast for Unsupervised Visual Representation Learning
- [speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
- Solve the problem of "no input file specified" when ThinkPHP starts
- Lesson 1: serpentine matrix
- 谷歌地图案例
- 2022软件测试工程师涨薪攻略,3年如何达到30K
- One article deals with the microstructure and instructions of class
猜你喜欢

Starting from 1.5, build a micro Service Framework -- log tracking traceid

关于MySQL的30条优化技巧,超实用

d3dx9_ How to repair 31.dll_ d3dx9_ 31. Solution to missing DLL

Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)

Nacos installation and service registration
![[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]](/img/f4/4d09dc05f5789b980ebd23cc352f8b.jpg)
[speech processing] speech signal denoising and denoising based on MATLAB low-pass filter [including Matlab source code 1709]

Vision Transformer (ViT)

查看网页最后修改时间方法以及原理简介

Overview of Fourier analysis

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
随机推荐
First, redis summarizes the installation types
Nacos installation and service registration
Why does the C# compiler allow an explicit cast between IEnumerable&lt; T&gt; and TAlmostAnything?
Thinkphp5.1 cross domain problem solving
Finally understand what dynamic planning is
Ultrasonic sensor flash | LEGO eV3 Teaching
Roman numeral to integer
fibonacci search
【Note17】PECI(Platform Environment Control Interface)
Binary tree (II) -- code implementation of heap
鏈錶之雙指針(快慢指針,先後指針,首尾指針)
Boring boring
Distributed solution selection
Go language learning tutorial (XV)
Activate function and its gradient
[speech processing] speech signal denoising based on Matlab GUI Hanning window fir notch filter [including Matlab source code 1711]
Go语言学习教程(十五)
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
a-tree 树的全部展开和收起
EasyCVR集群部署如何解决项目中的海量视频接入与大并发需求?