提交说明: 更新了LeetCode解题代码,包括股票买卖问题、单调栈问题以及字符串编辑距离问题。

This commit is contained in:
whaifree 2024-04-16 15:43:05 +08:00
parent 29eb97e14c
commit 3b19c764d8
4 changed files with 237 additions and 1 deletions

View File

@ -0,0 +1,59 @@
package cn.whaifree.leetCode.Dynamic;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/16 11:22
* @注释
*/
public class LeetCode64 {
@Test
public void test()
{
int[][] grid = new int[][] { {1,2,3}, {4,5,6} };
Solution solution = new Solution();
int i = solution.minPathSum(grid);
System.out.println(i);
}
class Solution {
/**
* dp
* int[i][j]表示到该点所需的最最小数字总和
*
* @param grid
* @return
*/
public int minPathSum(int[][] grid) {
int h = grid.length;
int w = grid[0].length;
int[][] dp = new int[h][w];
dp[0][0] = grid[0][0];
// dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j]
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
if (i == 0 && j == 0) {
continue;
}
if (j == 0 && i != 0) {
dp[i][j] = dp[i - 1][j] + grid[i][j];
continue;
}
if (i == 0 && j != 0) {
dp[i][j] = dp[i][j - 1] + grid[i][j];
continue;
}
dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j];
}
}
return dp[h - 1][w - 1];
}
}
}

View File

@ -222,3 +222,128 @@ class LeetCode121_ {
} }
} }
class LeetCode309_ {
class Solution {
public int maxProfit(int[] prices) {
// 卖出股票后你无法在第二天买入股票 (即冷冻期为 1 )
/**
* boolean[] flag
* flag[i] 表示第i天是否卖出获得最大利润
*
* s2 s3
* dp[i][0] 表示 第i天手里不持有股票,i-1也不持有表明在i-1之前卖出股票 手里有的钱
* - i不持有 i-1 不持有 i-2不持有 dp[i-2][0]
* - i不持有 i-1 不持有 i-2持有i-1卖出 dp[i-2][2] + prices[i-1]
*
* s4
* dp[i][1] 表示i不持有i-1持有 手里有的钱
* - 今天为冷冻期 i卖出 dp[i-1][2] + prices[i]
*
* s1
* dp[i][2] 表示 第i天持有股票 手里有的钱
* - i-1有 dp[i-1][2]
* - 前一天是冷冻期 i和i-1都不持有 dp[i-1][0] - price[i]
* - 当天是冷冻期 i不持有 i-1持有 dp[i-1][1] - price[i]
*
*/
int[][] dp = new int[prices.length][2];
boolean[] flag = new boolean[prices.length];
for (int i = 1; i < prices.length; i++) {
if (dp[i - 1][0] > dp[i - 1][1] + prices[i]) {
dp[i][0] = dp[i - 1][0];
}else {
dp[i][0] = dp[i - 1][1] + prices[i];
flag[i] = true;
}
// if
}
return 1;
}
}
class Solution1 {
/**
* 状态
* 1. 持有dp[i][0]
* - i-1就持有 dp[i-1][0]
* - 当天买入
* - 前一天是冷冻期 dp[i-1][3] - price[i]
* - 前一天不是冷冻期 dp[i-1][1] - price[i]
* 2. 不持有
* - i-1就不持有,i-2或之前卖出过股票 dp[i][1]
* - i-1不持有 i-2不持有 dp[i-1][1]
* - i-1不持有 i-2持有表明i-1卖出了 dp[i-1][2] + price[i]
* - 今天卖出 dp[i][2]
* - dp[i-1][0]+price[i]
*
* 3. 冷冻dp[i][3]
* - i-1卖出 dp[i-1][2]
* @param prices
* @return
*/
public int maxProfit(int[] prices) {
int[][] dp = new int[prices.length][4];
dp[0][0] -= prices[0];
for (int i = 1; i < prices.length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], Math.max(dp[i - 1][3] - prices[i], dp[i - 1][1] - prices[i]));
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][3]);
dp[i][2] = dp[i - 1][0] + prices[i];
dp[i][3] = dp[i - 1][2];
}
return Math.max(
dp[prices.length - 1][3],
Math.max(
dp[prices.length - 1][1],
dp[prices.length - 1][2]
)
);
}
/**
* dp[i][0] 表示第i天手里没有股票的 手里的现金
* 1. 当天卖出 dp[i-1][1] + prices[i]
* 2. i-1天就没有
* dp[i-1][0]
* dp[i][1] 表示第i天手里有股票的 手里的现金
* 1. 刚刚买入考虑2天前手里没有股票i-1天为冷冻期 dp[i-2][0] - prices[i]
* 2. i-1就持有 dp[i-1][1]
*
* 初始化
* dp[0][0] = 0
* dp[0][1] = -prices[0]
* dp[1][0] = Math.max(dp[0][0], dp[0][1] + prices[0]);
* dp[1][1] = Math.max(dp[0][1], dp[0][0]-prices[0]);
*
* @param prices
* @return
*/
public int maxProfit1(int[] prices) {
if(prices.length==1){
return 0;
}
int[][] dp = new int[prices.length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
dp[1][0] = Math.max(dp[0][0], dp[0][1] + prices[1]);
dp[1][1] = Math.max(dp[0][1], dp[0][0] - prices[1]);
for (int i = 2; 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 - 2][0] - prices[i]);
}
return dp[prices.length - 1][0];
}
}
public static void main(String[] args) {
int[] prices = new int[]{1, 2, 3, 0, 2};
System.out.println(new LeetCode309_().new Solution1().maxProfit1(prices));
}
}

View File

@ -0,0 +1,53 @@
package cn.whaifree.leetCode.LeetCode;
import org.junit.Test;
/**
* @version 1.0
* @Author whai文海
* @Date 2024/4/16 11:45
* @注释
*/
public class LeetCode72 {
@Test
public void test()
{
String word1 = "horse";
String word2 = "ros";
int res = new Solution().minDistance(word1, word2);
System.out.println(res);
}
class Solution {
public int minDistance(String word1, String word2) {
// dp[i][j] 表示将word1的第i个位置字符 替换为 word2的第j个字符所需的最小操作数
// - 删除 dp[i-1][j]
// - 增加 dp[i][j-1]
// - 替换 dp[i-1][j-1]
// dp[i][j] = min +1
int wL1 = word1.length();
int wL2 = word2.length();
int[][] dp = new int[wL1 + 1][wL2 + 1];
for (int i = 0; i <= wL1; i++) {
dp[i][0] = i;
}
for (int i = 0; i <= wL2; i++) {
dp[0][i] = i;
}
for (int i = 1; i <= wL1; i++) {
for (int j = 1; j <= wL2; j++) {
if (word1.charAt(i - 1) == word2.charAt(j - 1)){
// 如果对应位置两个字母相等不用做任何操作直接使用dp[i - 1][j - 1]
dp[i][j] = dp[i - 1][j - 1];
} else {
dp[i][j] = Math.min(Math.min(dp[i - 1][j - 1], dp[i][j - 1]), dp[i - 1][j]) + 1;
}
}
}
return dp[wL1][wL2];
}
}
}

View File

@ -21,7 +21,6 @@ public class LeetCode739 {
Solution solution = new Solution(); Solution solution = new Solution();
int[] res = solution.dailyTemperatures(temperatures); int[] res = solution.dailyTemperatures(temperatures);
System.out.println(Arrays.toString(res)); System.out.println(Arrays.toString(res));
} }
class Solution { class Solution {