Merge pull request #912 from spacker-343/master

修改了121.买卖股票的最佳时机的java题解
This commit is contained in:
程序员Carl
2021-11-24 15:04:14 +08:00
committed by GitHub
4 changed files with 101 additions and 59 deletions

View File

@ -195,23 +195,26 @@ public:
## 其他语言版本 ## 其他语言版本
Java Java
> 贪心法:
```java ```java
// 贪心思路
class Solution { class Solution {
public int maxProfit(int[] prices) { public int maxProfit(int[] prices) {
int minprice = Integer.MAX_VALUE; // 找到一个最小的购入点
int maxprofit = 0; int low = Integer.MAX_VALUE;
// res不断更新直到数组循环完毕
int res = 0;
for(int i = 0; i < prices.length; i++){ for(int i = 0; i < prices.length; i++){
if (prices[i] < minprice) { low = Math.min(prices[i], low);
minprice = prices[i]; res = Math.max(prices[i] - low, res);
} else if (prices[i] - minprice > maxprofit) {
maxprofit = prices[i] - minprice;
} }
} return res;
return maxprofit;
} }
} }
``` ```
> 动态规划:版本一
```java ```java
// 解法1 // 解法1
class Solution { class Solution {
@ -233,29 +236,29 @@ class Solution {
} }
``` ```
> 动态规划:版本二
``` java ``` java
class Solution { // 动态规划解法 class Solution {
public int maxProfit(int[] prices) { public int maxProfit(int[] prices) {
// 可交易次数 int[] dp = new int[2];
int k = 1; dp[0] = -prices[0];
// [天数][交易次数][是否持有股票] dp[1] = 0;
int[][][] dp = new int[prices.length][k + 1][2]; // 可以参考斐波那契问题的优化方式
// dp[0] 和 dp[1], 其实是第 0 天的数据
// bad case // 所以我们从 i=1 开始遍历数组,一共有 prices.length 天,
dp[0][0][0] = 0; // 所以是 i<=prices.length
dp[0][0][1] = Integer.MIN_VALUE; for (int i = 1; i <= prices.length; i++) {
dp[0][1][0] = Integer.MIN_VALUE; // 前一天持有;或当天买入
dp[0][1][1] = -prices[0]; dp[0] = Math.max(dp[0], -prices[i - 1]);
// 如果 dp[0] 被更新,那么 dp[1] 肯定会被更新为正数的 dp[1]
for (int i = 1; i < prices.length; i++) { // 而不是 dp[0]+prices[i-1]==0 的0
for (int j = k; j >= 1; j--) { // 所以这里使用会改变的dp[0]也是可以的
// dp公式 // 当然 dp[1] 初始值为 0 ,被更新成 0 也没影响
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]); // 前一天卖出;或当天卖出, 当天要卖出,得前一天持有才行
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]); dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);
} }
} return dp[1];
return dp[prices.length - 1][k][0] > 0 ? dp[prices.length - 1][k][0] : 0;
} }
} }
``` ```

View File

@ -167,6 +167,25 @@ class Solution { // 动态规划
} }
``` ```
```java
// 优化空间
class Solution {
public int maxProfit(int[] prices) {
int[] dp=new int[2];
// 0表示持有1表示卖出
dp[0]=-prices[0];
dp[1]=0;
for(int i=1; i<=prices.length; i++){
// 前一天持有; 或当天卖出然后买入
dp[0]=Math.max(dp[0], dp[1]-prices[i-1]);
// 前一天卖出; 或当天卖出,当天卖出,得先持有
dp[1]=Math.max(dp[1], dp[0]+prices[i-1]);
}
return dp[1];
}
}
```
### Python ### Python

View File

@ -188,7 +188,7 @@ dp[1] = max(dp[1], dp[0] - prices[i]); 如果dp[1]取dp[1],即保持买入股
## 其他语言版本 ## 其他语言版本
Java ### Java
```java ```java
// 版本一 // 版本一
@ -221,25 +221,30 @@ class Solution {
// 版本二: 空间优化 // 版本二: 空间优化
class Solution { class Solution {
public int maxProfit(int[] prices) { public int maxProfit(int[] prices) {
int len = prices.length; int[] dp=new int[4];
int[] dp = new int[5]; // 存储两天的状态就行了
dp[1] = -prices[0]; // dp[0]代表第一次买入
dp[3] = -prices[0]; dp[0]=-prices[0];
// dp[1]代表第一次卖出
for (int i = 1; i < len; i++) { dp[1]=0;
dp[1] = Math.max(dp[1], dp[0] - prices[i]); // dp[2]代表第二次买入
dp[2] = Math.max(dp[2], dp[1] + prices[i]); dp[2]=-prices[0];
dp[3] = Math.max(dp[3], dp[2] - prices[i]); // dp[3]代表第二次卖出
dp[4] = Math.max(dp[4], dp[3] + prices[i]); dp[3]=0;
for(int i=1; i<=prices.length; i++){
// 要么保持不变,要么没有就买,有了就卖
dp[0]=Math.max(dp[0], -prices[i-1]);
dp[1]=Math.max(dp[1], dp[0]+prices[i-1]);
// 这已经是第二天了,所以得加上前一天卖出去的价格
dp[2]=Math.max(dp[2], dp[1]-prices[i-1]);
dp[3]=Math.max(dp[3], dp[2]+prices[i-1]);
} }
return dp[3];
return dp[4];
} }
} }
``` ```
### Python
Python
> 版本一: > 版本一:
```python ```python
@ -308,7 +313,7 @@ func max(a,b int)int{
JavaScript ### JavaScript
> 版本一: > 版本一:
@ -347,7 +352,7 @@ const maxProfit = prices => {
}; };
``` ```
Go: ### Go
> 版本一: > 版本一:
```go ```go
@ -381,5 +386,7 @@ func max(a, b int) int {
``` ```
----------------------- -----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div> <div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -222,19 +222,32 @@ class Solution {
//版本三:一维 dp数组 //版本三:一维 dp数组
class Solution { class Solution {
public int maxProfit(int k, int[] prices) { public int maxProfit(int k, int[] prices) {
//在版本二的基础上,由于我们只关心前一天的股票买入情况,所以只存储前一天的股票买入情况 if(prices.length == 0){
if(prices.length==0)return 0; return 0;
int[] dp=new int[2*k+1];
for (int i = 1; i <2*k ; i+=2) {
dp[i]=-prices[0];
} }
for (int i = 0; i <prices.length ; i++) { if(k == 0){
for (int j = 1; j <2*k ; j+=2) { return 0;
dp[j]=Math.max(dp[j],dp[j-1]-prices[i]); }
dp[j+1]=Math.max(dp[j+1],dp[j]+prices[i]); // 其实就是123题的扩展123题只用记录2天的状态
// 这里记录k天的状态就行了
// 每天都有买入,卖出两个状态,所以要乘 2
int[] dp = new int[2 * k];
// 按123题解题格式那样做一个初始化
for(int i = 0; i < dp.length / 2; i++){
dp[i * 2] = -prices[0];
}
for(int i = 1; i <= prices.length; i++){
dp[0] = Math.max(dp[0], -prices[i - 1]);
dp[1] = Math.max(dp[1], dp[0] + prices[i - 1]);
// 还是与123题一样与123题对照来看
// 就很容易啦
for(int j = 2; j < dp.length; j += 2){
dp[j] = Math.max(dp[j], dp[j - 1] - prices[i-1]);
dp[j + 1] = Math.max(dp[j + 1], dp[j] + prices[i - 1]);
} }
} }
return dp[2*k]; // 返回最后一天卖出状态的结果就行了
return dp[dp.length - 1];
} }
} }
``` ```