当前位置:网站首页>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];
}
}
边栏推荐
- Nacos installation and service registration
- 二叉树(二)——堆的代码实现
- Leetcode daily question 1189 The maximum number of "balloons" simple simulation questions~
- 一文搞定class的微观结构和指令
- 如何快速理解复杂业务,系统思考问题?
- Overview of Fourier analysis
- Metaverse ape received $3.5 million in seed round financing from negentropy capital
- 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
- Qtquick3d real time reflection
- [untitled]
猜你喜欢

Google Maps case

Tensor attribute statistics

透彻理解JVM类加载子系统

50. Pow(x, n). O(logN) Sol

700. Search in a Binary Search Tree. Sol

Golang writes the opening chapter of selenium framework

我把开源项目alinesno-cloud-service关闭了

Paddy serving v0.9.0 heavy release multi machine multi card distributed reasoning framework

Qtquick3d real time reflection

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
随机推荐
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
Global and Chinese market of diesel fire pump 2022-2028: Research Report on technology, participants, trends, market size and share
航海日答题小程序之航海知识竞赛初赛
一文搞定JVM的内存结构
audiopolicy
Event trigger requirements of the function called by the event trigger
分布式解决方案之TCC
查看网页最后修改时间方法以及原理简介
audiopolicy
Nacos 的安装与服务的注册
Arduino 测量交流电流
[groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
The difference between MVVM and MVC
一文搞定class的微观结构和指令
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Finally understand what dynamic planning is
344. Reverse String. Sol
我把开源项目alinesno-cloud-service关闭了
Go language learning tutorial (XV)
Golang writes the opening chapter of selenium framework