Leetcode (one question per day)

Longest repeating subarray

Title description:

Insert picture description here

Thinking analysis: dynamic planning directly kills (for ideas, see the previous blog Huawei machine test (2) )

Directly on the code:

class Solution {
    
    
public:
    int findLength(vector<int>& A, vector<int>& B) {
    
    
           int n = A.size(), m = B.size();
           vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
            int ans = 0;
            for (int i = n - 1; i >= 0; i--) {
    
    
                for (int j = m - 1; j >= 0; j--) {
    
    
                    dp[i][j] = A[i] == B[j] ? dp[i + 1][j + 1] + 1 : 0;
                    ans = max(ans, dp[i][j]);
                }
            }
            return ans;
        }
};

Guess you like

Origin blog.csdn.net/qq_41606378/article/details/108599658