Code Caprice Training Camp day53| 1143. Longest Common Subsequence 1035. Disjoint Lines 53. Maximum Subsequence Sum...

@TOC


foreword

Code Random Recording Algorithm Training Camp day53


1. Leetcode 1143. Longest Common Subsequence

1. Topic

Given two strings text1 and text2, return the length of the longest common subsequence of these two strings. Returns 0 if no common subsequence exists.

A subsequence of a string refers to such a new string: it is a new string formed by deleting some characters (or not deleting any characters) from the original string without changing the relative order of the characters.

例如,"ace" 是 "abcde" 的子序列,但 "aec" 不是 "abcde" 的子序列。

A common subsequence of two strings is a subsequence that both strings have in common.

Example 1:

Input: text1 = "abcde", text2 = "ace" Output: 3
Explanation: The longest common subsequence is "ace" and its length is 3.

Example 2:

Input: text1 = "abc", text2 = "abc" Output: 3 Explanation: The longest common subsequence is "abc", which has length 3.

Example 3:

Input: text1 = "abc", text2 = "def" Output: 0 Explanation: Two strings have no common subsequence, return 0.

hint:

1 <= text1.length, text2.length <= 1000
text1 和 text2 仅由小写英文字符组成。

Source: LeetCode Link: https://leetcode.cn/problems/longest-common-subsequence

2. Problem-solving ideas

Method 1: Dynamic Programming

The longest common subsequence problem is a typical two-dimensional dynamic programming problem.

Assuming that the lengths of the strings text1text1​ and text2text2​ are mm and nn respectively, create a two-dimensional array dpdp of m+1m+1 rows n+1n+1 columns, where dp[i][j]dp[i][j] Indicates the length of the longest common subsequence of text1[0:i]text1​[0:i] and text2[0:j]text2​[0:j].

上述表示中,text1[0:i]text1​[0:i] 表示 text1text1​ 的长度为 ii 的前缀,text2[0:j]text2​[0:j] 表示 text2text2​ 的长度为 jj 的前缀。

Consider the edge cases of dynamic programming:

当 i=0i=0 时,text1[0:i]text1​[0:i] 为空,空字符串和任何字符串的最长公共子序列的长度都是 00,因此对任意 0≤j≤n0≤j≤n,有 dp[0][j]=0dp[0][j]=0;

当 j=0j=0 时,text2[0:j]text2​[0:j] 为空,同理可得,对任意 0≤i≤m0≤i≤m,有 dp[i][0]=0dp[i][0]=0。

So the boundary case of dynamic programming is: when i=0i=0 or j=0j=0, dp[i][j]=0dp[i][j]=0.

When i>0i>0 and j>0j>0, consider the calculation of dp[i][j]dp[i][j]:

当 text1[i−1]=text2[j−1]text1​[i−1]=text2​[j−1] 时,将这两个相同的字符称为公共字符,考虑 text1[0:i−1]text1​[0:i−1] 和 text2[0:j−1]text2​[0:j−1] 的最长公共子序列,再增加一个字符(即公共字符)即可得到 text1[0:i]text1​[0:i] 和 text2[0:j]text2​[0:j] 的最长公共子序列,因此 dp[i][j]=dp[i−1][j−1]+1dp[i][j]=dp[i−1][j−1]+1。

当 text1[i−1]≠text2[j−1]text1​[i−1]​=text2​[j−1] 时,考虑以下两项:

    text1[0:i−1]text1​[0:i−1] 和 text2[0:j]text2​[0:j] 的最长公共子序列;

    text1[0:i]text1​[0:i] 和 text2[0:j−1]text2​[0:j−1] 的最长公共子序列。

要得到 text1[0:i]text1​[0:i] 和 text2[0:j]text2​[0:j] 的最长公共子序列,应取两项中的长度较大的一项,因此 dp[i][j]=max⁡(dp[i−1][j],dp[i][j−1])dp[i][j]=max(dp[i−1][j],dp[i][j−1])。

From this, the following state transition equation can be obtained:

dp[i][j]={dp[i−1][j−1]+1,text1[i−1]=text2[j−1]max⁡(dp[i−1][j],dp[i][j−1]),text1[i−1]≠text2[j−1]dp[i][j]={dp[i−1][j−1]+1,max(dp[i−1][j],dp[i][j−1]),​text1​[i−1]=text2​[j−1]text1​[i−1]​=text2​[j−1]​

Finally, dp[m][n]dp[m][n] is the length of the longest common subsequence of text1text1​ and text2text2​.

3. Code implementation

```java class Solution { public int longestCommonSubsequence(String text1, String text2) { int m = text1.length(), n = text2.length(); int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { char c1 = text1.charAt(i - 1); for (int j = 1; j <= n; j++) { char c2 = text2.charAt(j - 1); if (c1 == c2) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; } }

```

2. Leetcode 1035. Disjoint lines

1. Topic

Write the integers in nums1 and nums2 in the given order on two separate horizontal lines.

Now, you can draw some straight lines connecting two numbers nums1[i] and nums2[j], these lines need to satisfy both:

nums1[i] == nums2[j]
且绘制的直线不与任何其他连线(非水平线)相交。

Note that lines cannot intersect even at endpoints: each number can only belong to one line.

Draws lines in this way and returns the maximum number of lines that can be drawn.

Example 1:

Input: nums1 = [1,4,2], nums2 = [1,2,4] Output: 2 Explanation: Two non-intersecting lines can be drawn, as shown in the figure above. But the third disjoint line cannot be drawn because the line from nums1[1]=4 to nums2[2]=4 will intersect the line from nums1[2]=2 to nums2[1]=2.

Example 2:

Input: nums1 = [2,5,1,2,5], nums2 = [10,5,2,1,5,2] Output: 3

Example 3:

Input: nums1 = [1,3,7,1,7,5], nums2 = [1,9,2,5,1] Output: 2

hint:

1 <= nums1.length, nums2.length <= 500
1 <= nums1[i], nums2[j] <= 2000

Source: LeetCode Link: https://leetcode.cn/problems/uncrossed-lines

2. Problem-solving ideas

Method 1: Dynamic Programming

Given two arrays nums1nums1​ and nums2nums2​, when nums1[i]=nums2[j]nums1​[i]=nums2​[j], a straight line can be used to connect nums1[i]nums1​[i] and nums2 [j]nums2​[j]. Suppose a total of kk disjoint straight lines are drawn, among which the xxth straight line connects nums1[ix]nums1​[ix​] and nums2[jx]nums2​[jx​], then for any 1≤x≤k1≤x ≤k have nums1[ix]=nums2[jx]nums1​[ix​]=nums2​[jx​], where i1

The above kk disjoint straight lines respectively connect the kk pairs of equal elements of the arrays nums1nums1​ and nums2nums2​, and the relative order of the kk pairs of equal elements in the two arrays is consistent. Therefore, the kk pairs of equal elements The sequence of elements is the common subsequence of the arrays nums1nums1​ and nums2nums2​. To calculate the maximum number of lines that can be drawn, it is to calculate the length of the longest common subsequence of the arrays nums1nums1​ and nums2nums2​. The longest common subsequence problem is a typical two-dimensional dynamic programming problem.

Assuming that the lengths of the arrays nums1nums1​ and nums2nums2​ are mm and nn respectively, create a two-dimensional array dpdp with m+1m+1 rows and n+1n+1 columns, where dp[i][j]dp[i][j] means The length of the longest common subsequence of nums1[0:i]nums1​[0:i] and nums2[0:j]nums2​[0:j].

上述表示中,nums1[0:i]nums1​[0:i] 表示数组 nums1nums1​ 的长度为 ii 的前缀,nums2[0:j]nums2​[0:j] 表示数组 nums2nums2​ 的长度为 jj 的前缀。

Consider the edge cases of dynamic programming:

当 i=0i=0 时,nums1[0:i]nums1​[0:i] 为空,空数组和任何数组的最长公共子序列的长度都是 00,因此对任意 0≤j≤n0≤j≤n,有 dp[0][j]=0dp[0][j]=0;

当 j=0j=0 时,nums2[0:j]nums2​[0:j] 为空,同理可得,对任意 0≤i≤m0≤i≤m,有 dp[i][0]=0dp[i][0]=0。

So the boundary case of dynamic programming is: when i=0i=0 or j=0j=0, dp[i][j]=0dp[i][j]=0.

When i>0i>0 and j>0j>0, consider the calculation of dp[i][j]dp[i][j]:

当 nums1[i−1]=nums2[j−1]nums1​[i−1]=nums2​[j−1] 时,将这两个相同的元素称为公共元素,考虑 nums1[0:i−1]nums1​[0:i−1] 和 nums2[0:j−1]nums2​[0:j−1] 的最长公共子序列,再增加一个元素(即公共元素)即可得到 nums1[0:i]nums1​[0:i] 和 nums2[0:j]nums2​[0:j] 的最长公共子序列,因此 dp[i][j]=dp[i−1][j−1]+1dp[i][j]=dp[i−1][j−1]+1。

当 nums1[i−1]≠nums2[j−1]nums1​[i−1]​=nums2​[j−1] 时,考虑以下两项:

    nums1[0:i−1]nums1​[0:i−1] 和 nums2[0:j]nums2​[0:j] 的最长公共子序列;

    nums1[0:i]nums1​[0:i] 和 nums2[0:j−1]nums2​[0:j−1] 的最长公共子序列。

要得到 nums1[0:i]nums1​[0:i] 和 nums2[0:j]nums2​[0:j] 的最长公共子序列,应取两项中的长度较大的一项,因此 dp[i][j]=max⁡(dp[i−1][j],dp[i][j−1])dp[i][j]=max(dp[i−1][j],dp[i][j−1])。

From this, the following state transition equation can be obtained:

dp[i][j]={dp[i−1][j−1]+1,nums1[i−1]=nums2[j−1]max⁡(dp[i−1][j],dp[i][j−1]),nums1[i−1]≠nums2[j−1]dp[i][j]={dp[i−1][j−1]+1,max(dp[i−1][j],dp[i][j−1]),​nums1​[i−1]=nums2​[j−1]nums1​[i−1]​=nums2​[j−1]​

Finally, dp[m][n]dp[m][n] is the length of the longest common subsequence of the arrays nums1nums1​ and nums2nums2​, that is, the maximum number of lines that can be drawn.

fig1

3. Code implementation

```java class Solution { public int maxUncrossedLines(int[] nums1, int[] nums2) { int m = nums1.length, n = nums2.length; int[][] dp = new int[m + 1][n + 1]; for (int i = 1; i <= m; i++) { int num1 = nums1[i - 1]; for (int j = 1; j <= n; j++) { int num2 = nums2[j - 1]; if (num1 == num2) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[m][n]; } }

```

3. Leetcode 53. Maximum subsequence sum

1. Topic

Given an integer array nums, please find a continuous subarray with the maximum sum (the subarray contains at least one element), and return the maximum sum.

A subarray is a contiguous part of an array.

Example 1:

Input: nums = [-2,1,-3,4,-1,2,1,-5,4] Output: 6 Explanation: The sum of consecutive subarrays [4,-1,2,1] is the largest, which is 6.

Example 2:

Input: nums = [1] Output: 1

Example 3:

Input: nums = [5,4,-1,7,8] Output: 23

hint:

1 <= nums.length <= 105
-104 <= nums[i] <= 104

Source: LeetCode Link: https://leetcode.cn/problems/maximum-subarray

2. Problem-solving ideas

Method 1: Dynamic Programming

ideas and algorithms

Suppose the length of the numsnums array is nn, with subscripts from 00 to n−1n−1.

We use f(i)f(i) to represent the "maximum sum of consecutive sub-arrays" ending with the ii number, then obviously the answer we require is:

max⁡0≤i≤n−1{f(i)}0≤i≤n−1max​{f(i)}

So we just need to find f(i)f(i) for each position, and return the maximum value in the ff array. So how do we find f(i)f(i)? We can consider whether nums[i]nums[i] becomes a section alone or join the section corresponding to f(i−1)f(i−1), which depends on nums[i]nums[i] and f(i−1 )+nums[i]f(i−1)+nums[i], we hope to get a larger one, so we can write such a dynamic programming transfer equation:

f(i)=max⁡{f(i−1)+nums[i],nums[i]}f(i)=max{f(i−1)+nums[i],nums[i]}

It is not difficult to give an implementation of time complexity O(n)O(n) and space complexity O(n)O(n), that is, use an ff array to store the value of f(i)f(i), and use A loop finds all f(i)f(i). Considering that f(i)f(i) is only related to f(i−1)f(i−1), we can use only one variable prepre to maintain f(i) for the current f(i)f(i) −1) What is the value of f(i−1), so that the space complexity is reduced to O(1)O(1), which is a bit similar to the idea of ​​"rolling array".

3. Code implementation

```java class Solution { public int maxSubArray(int[] nums) { int pre = 0, maxAns = nums[0]; for (int x : nums) { pre = Math.max(pre + x, x); maxAns = Math.max(maxAns, pre); } return maxAns; } }

```

Guess you like

Origin blog.csdn.net/HHX_01/article/details/131285449