代码随想录_动态规划(三)
1. 300 最长递增子序列
给你一个整数数组 nums ,找到其中最长严格递增子序列的长度。
子序列是由数组派生而来的序列,删除(或不删除)数组中的元素而不改变其余元素的顺序。例如,[3,6,2,7] 是数组 [0,3,1,6,2,2,7] 的子序列。
示例 1:
输入:nums = [10,9,2,5,3,7,101,18]
输出:4
解释:最长递增子序列是 [2,3,7,101],因此长度为 4 。
示例 2:
输入:nums = [0,1,0,3,2,3]
输出:4
示例 3:
输入:nums = [7,7,7,7,7,7,7]
输出:1
提示:
1 <= nums.length <= 2500
-10^4 <= nums[i] <= 104
思路
Java代码
import java.util.Arrays;
public class ThreeZeroZero {
public static void main(String[] args) {
int[] nums = new int[]{10,9,2,5,3,7,101,18};
System.out.println(lengthOfLIS(nums));
}
public static int lengthOfLIS(int[] nums) {
int n = nums.length;
int[] dp = new int[n];
Arrays.fill(dp, 1);
int res = 0;
for (int i = 1; i < n; i++) {
for (int j = 0; j < n; j++) {
if (nums[i] > nums[j]) dp[i] = Math.max(dp[i], dp[j] + 1);
}
res = Math.max(res, dp[i]);
}
return res;
}
}
2. 674 最长连续递增序列
给定一个未经排序的整数数组,找到最长且 连续递增的子序列,并返回该序列的长度。
连续递增的子序列 可以由两个下标 l 和 r(l < r)确定,如果对于每个 l <= i < r,都有 nums[i] < nums[i + 1] ,那么子序列 [nums[l], nums[l + 1], …, nums[r - 1], nums[r]] 就是连续递增子序列。
示例 1:
输入:nums = [1,3,5,4,7]
输出:3
解释:最长连续递增序列是 [1,3,5], 长度为3。尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为 5 和 7 在原数组里被 4 隔开。
示例 2:
输入:nums = [2,2,2,2,2]
输出:1
解释:最长连续递增序列是 [2], 长度为1。
提示:
0 <= nums.length <= 10^4
-10^9 <= nums[i] <= 10^9
思路
Java代码
import java.util.Arrays;
public class SixSevenFour {
public static void main(String[] args) {
int[] nums = new int[]{1,3,5,4,7};
System.out.println(findLengthOfLCIS(nums));
}
public static int findLengthOfLCIS(int[] nums) {
int[] dp = new int[nums.length];
Arrays.fill(dp,1);
int res = 1;
for (int i = 1; i < nums.length; i++) {
if (nums[i] > nums[i-1]) {
dp[i] = dp[i-1] + 1;
}
if (res < dp[i]) res = dp[i];
}
return res;
}
}
3. 1143 最长公共子序列
给定两个字符串 text1 和 text2,返回这两个字符串的最长公共子序列的长度。
一个字符串的 子序列 是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下删除某些字符(也可以不删除任何字符)后组成的新字符串。
例如,“ace” 是 “abcde” 的子序列,但 “aec” 不是 “abcde” 的子序列。两个字符串的「公共子序列」是这两个字符串所共同拥有的子序列。
若这两个字符串没有公共子序列,则返回 0。
示例 1:
输入:text1 = "abcde", text2 = "ace"
输出:3
解释:最长公共子序列是 "ace",它的长度为 3。
示例 2:
输入:text1 = "abc", text2 = "abc"
输出:3
解释:最长公共子序列是 "abc",它的长度为 3。
示例 3:
输入:text1 = "abc", text2 = "def"
输出:0
解释:两个字符串没有公共子序列,返回 0。
提示:
1 <= text1.length <= 1000
1 <= text2.length <= 1000 输入的字符串只含有小写英文字符。
思路
Java代码
public class OneOneFourThree {
public static void main(String[] args) {
String text1 = "abcde";
String text2 = "ace";
System.out.println(longestCommonSubsequence(text1, text2));
}
public static int longestCommonSubsequence(String text1, String text2) {
int n1 = text1.length();
int n2 = text2.length();
int[][] dp = new int[n1+1][n2+1];
for (int i = 0; i < text1.length(); i++) {
for (int j = 0; j < text2.length(); j++) {
if (text1.charAt(i) == text2.charAt(j)) {
dp[i+1][j+1] = dp[i][j] + 1;
} else {
dp[i+1][j+1] = Math.max(dp[i+1][j], dp[i][j+1]);
}
}
}
return dp[n1][n2];
}
}
4. 718 最长重复子数组
给两个整数数组 A 和 B ,返回两个数组中公共的、长度最长的子数组的长度。
示例:
输入:
A: [1,2,3,2,1]
B: [3,2,1,4,7]
输出:3
解释:长度最长的公共子数组是 [3, 2, 1] 。
提示:
1 <= len(A), len(B) <= 1000
0 <= A[i], B[i] < 100
思路
Java代码
public class SevenOneEight {
public static void main(String[] args) {
int[] nums1 = new int[]{1,2,3,2,1};
int[] nums2 = new int[]{3,2,1,4,7};
System.out.println(findLength(nums1, nums2));
}
public static int findLength(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
int[][] dp = new int[n1+1][n2+1];
int res = 0;
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
if (nums1[i] == nums2[j]) {
dp[i+1][j+1] = dp[i][j] + 1;
}
if (res < dp[i+1][j+1]) res = dp[i+1][j+1];
}
}
return res;
}
}
5. 1035.不相交的线
在两条独立的水平线上按给定的顺序写下 nums1 和 nums2 中的整数。
现在,可以绘制一些连接两个数字 nums1[i] 和 nums2[j] 的直线,这些直线需要同时满足:
nums1[i] == nums2[j]
且绘制的直线不与任何其他连线(非水平线)相交。
请注意,连线即使在端点也不能相交:每个数字只能属于一条连线。
以这种方法绘制线条,并返回可以绘制的最大连线数。

思路
Java代码
public class OneZeroThreeFive {
public static void main(String[] args) {
int[] nums1 = new int[]{1,4,2};
int[] nums2 = new int[]{1,2,4};
System.out.println(maxUncrossedLines(nums1, nums2));
}
public static int maxUncrossedLines(int[] nums1, int[] nums2) {
int n1 = nums1.length;
int n2 = nums2.length;
int[][] dp = new int[n1+1][n2+1];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
if (nums1[i] == nums2[j]) {
dp[i+1][j+1] = dp[i][j] + 1;
} else {
dp[i+1][j+1] = Math.max(dp[i+1][j], dp[i][j+1]);
}
}
}
return dp[n1][n2];
}
}
6. 53. 最大子序和
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
示例:
输入: [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
思路
Java代码
public class FiveThree {
public static void main(String[] args) {
int[] nums = new int[]{-2,1,-3,4,-1,2,1,-5,4};
System.out.println(maxSubArray(nums));
}
public static int maxSubArray(int[] nums) {
int[] dp = new int[nums.length];
dp[0] = nums[0];
int res = nums[0];
for (int i = 1; i < nums.length; i++) {
dp[i] = Math.max(dp[i-1] + nums[i], nums[i]);
res = Math.max(dp[i], res);
}
return res;
}
}
7. 392 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列。
字符串的一个子序列是原始字符串删除一些(也可以不删除)字符而不改变剩余字符相对位置形成的新字符串。(例如,"ace"是"abcde"的一个子序列,而"aec"不是)。
示例 1:
输入:s = "abc", t = "ahbgdc"
输出:true
示例 2:
输入:s = "axc", t = "ahbgdc"
输出:false
提示:
0 <= s.length <= 100
0 <= t.length <= 10^4
两个字符串都只由小写字符组成。
思路
Java代码
public class ThreeNineTwo {
public static void main(String[] args) {
String s = "abc";
String t = "ahbgdc";
System.out.println(isSubsequence(s, t));
}
public static boolean isSubsequence(String s, String t) {
int n1 = s.length();
int n2 = t.length();
int[][] dp = new int[n1+1][n2+1];
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
if (s.charAt(i) == t.charAt(j)) {
dp[i+1][j+1] = dp[i][j] + 1;
} else {
dp[i+1][j+1] = Math.max(dp[i+1][j], dp[i][j+1]);
}
}
}
if (dp[n1][n2] == n1) return true;
return false;
}
}
8. 115 不同的子序列
给定一个字符串 s 和一个字符串 t ,计算在 s 的子序列中 t 出现的个数。
字符串的一个 子序列 是指,通过删除一些(也可以不删除)字符且不干扰剩余字符相对位置所组成的新字符串。(例如,“ACE” 是 “ABCDE” 的一个子序列,而 “AEC” 不是)
题目数据保证答案符合 32 位带符号整数范围。

提示
0 <= s.length, t.length <= 1000
s 和 t 由英文字母组成
思路
Java代码
public class OneOneFive {
/*
假设当前遍历到的 t 是 bag,s 是 babga,此时 s 又来了一个 g, 和当前 t 的最后一个元素相同了。
这个时候新的 babgag 含有 bag 的数量是在 babga 原本包含的 bag 数量(dp【i - 1】【j】)的基础上,增加了使用新来的 g 新组成的 bag 数量。
新来的 g 能组成多少个bag,其实是原本的 babga 包含多少个 ba。也就是 dp【i - 1】【j - 1】。
*/
// dp[i][j] 指的是 s字符串中 第 i 个子序列前 包含的 t字符串中 第 j 个子序列前的个数
public static void main(String[] args) {
String s = "rabbbit";
String t = "rabbit";
System.out.println(numDistinct(s, t));
}
public static int numDistinct(String s, String t) {
int n1 = s.length();
int n2 = t.length();
int[][] dp = new int[n1+1][n2+1];
for (int i = 0; i <= n1; i++) dp[i][0] = 1;
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
if (s.charAt(i) == t.charAt(j)) {
dp[i+1][j+1] = dp[i][j+1] + dp[i][j];
} else {
dp[i+1][j+1] = dp[i][j+1];
}
}
}
return dp[n1][n2];
}
}
9. 583 两个字符串的删除操作
给定两个单词 word1 和 word2,找到使得 word1 和 word2 相同所需的最小步数,每步可以删除任意一个字符串中的一个字符。
示例:
输入: "sea", "eat"
输出: 2
解释: 第一步将"sea"变为"ea",第二步将"eat"变为"ea"
思路
Java代码
import java.util.Arrays;
public class FiveEightThree {
public static void main(String[] args) {
String word1 = "sea";
String word2 = "eat";
System.out.println(minDistance(word1, word2));
}
public static int minDistance(String word1, String word2) {
char[] arr1 = word1.toCharArray();
char[] arr2 = word2.toCharArray();
int[][] dp = new int[arr1.length+1][arr2.length+1];
for (int i = 0; i < arr1.length + 1; i++) dp[i][0] = i;
for (int j = 0; j < arr2.length + 1; j++) dp[0][j] = j;
for (int i = 0; i < word1.length(); i++) {
for (int j = 0; j < word2.length(); j++) {
if (word1.charAt(i) == word2.charAt(j)) {
dp[i+1][j+1] = dp[i][j];
} else {
dp[i+1][j+1] = Math.min(dp[i+1][j], dp[i][j+1]) + 1;
}
}
}
return dp[arr1.length][arr2.length];
}
}
10. 72 编辑距离
给你两个单词 word1 和 word2,请你计算出将 word1 转换成 word2 所使用的最少操作数 。
你可以对一个单词进行如下三种操作:
插入一个字符
删除一个字符
替换一个字符
示例 1:
输入:word1 = "horse", word2 = "ros"
输出:3
解释: horse -> rorse (将 'h' 替换为 'r') rorse -> rose (删除 'r') rose -> ros (删除 'e')
示例 2:
输入:word1 = "intention", word2 = "execution"
输出:5
解释: intention -> inention (删除 't') inention -> enention (将 'i' 替换为 'e') enention -> exention (将 'n' 替换为 'x') exention -> exection (将 'n' 替换为 'c') exection -> execution (插入 'u')
提示:
0 <= word1.length, word2.length <= 500
word1 和 word2 由小写英文字母组成
思路
Java代码
public class SevenTwo {
public static void main(String[] args) {
String word1 = "horse";
String word2 = "ros";
System.out.println(minDistance(word1, word2));
}
public static int minDistance(String word1, String word2) {
int n1 = word1.length();
int n2 = word2.length();
int[][] dp = new int[n1+1][n2+1];
for (int i = 0; i < n1 + 1; i++) dp[i][0] = i;
for (int j = 0; j < n2 + 1; j++) dp[0][j] = j;
for (int i = 0; i < word1.length(); i++) {
for (int j = 0; j < word2.length(); j++) {
if (word1.charAt(i) == word2.charAt(j)) {
dp[i+1][j+1] = dp[i][j];
} else {
dp[i+1][j+1] = Math.min(Math.min(dp[i][j],dp[i+1][j]),dp[i][j+1]) + 1;
}
}
}
return dp[n1][n2];
}
}
11. 647 回文子串
给定一个字符串,你的任务是计算这个字符串中有多少个回文子串。
具有不同开始位置或结束位置的子串,即使是由相同的字符组成,也会被视作不同的子串。
示例 1:
输入:"abc"
输出:3
解释:三个回文子串: "a", "b", "c"。
示例 2:
输入:"aaa"
输出:6
解释:6个回文子串: "a", "a", "a", "aa", "aa", "aaa"
提示:输入的字符串长度不会超过 1000 。
思路
Java代码
public class SixFourSeven {
public static void main(String[] args) {
String s = "aaa";
System.out.println(countSubstrings(s));
}
public static int countSubstrings(String s) {
int n = s.length();
boolean[][] dp = new boolean[n][n];
int res = 0;
for (int i = n - 1; i >= 0; i--) {
for (int j = i; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
if (j - i <= 1) {
res++;
dp[i][j] = true;
} else {
if (dp[i+1][j-1]) {
res++;
dp[i][j] = true;
}
}
}
}
}
return res;
}
}
12. 516 最长回文子序列
给定一个字符串 s ,找到其中最长的回文子序列,并返回该序列的长度。可以假设 s 的最大长度为 1000 。
**示例 1: **
输入: "bbbab"
输出: 4
一个可能的最长回文子序列为 "bbbb"。
示例 2:
输入:"cbbd"
输出: 2
一个可能的最长回文子序列为 "bb"。
提示:
1 <= s.length <= 1000
s 只包含小写英文字母
思路
Java代码
public class FiveOneSix {
public static void main(String[] args) {
String s = "bbbab";
System.out.println(longestPalindromeSubseq(s));
}
public static int longestPalindromeSubseq(String s) {
int n = s.length();
int[][] dp = new int[n][n];
for (int i = 0; i < n; i++) dp[i][i] = 1;
for (int i = n - 2; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
if (s.charAt(i) == s.charAt(j)) {
dp[i][j] = dp[i+1][j-1] + 2;
} else {
dp[i][j] = Math.max(dp[i+1][j], dp[i][j-1]);
}
}
}
return dp[0][n-1];
}
}
773

被折叠的 条评论
为什么被折叠?



