leetcode 718. Maximum Length of Repeated Subarray | 718. 最长重复子数组(动态规划)
生活随笔
收集整理的這篇文章主要介紹了
leetcode 718. Maximum Length of Repeated Subarray | 718. 最长重复子数组(动态规划)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目
https://leetcode.com/problems/maximum-length-of-repeated-subarray/
題解
Dynamic Programming [Accepted]
參考:官方題解
Intuition and Algorithm
Since a common subarray of A and B must start at some A[i] and B[j], let dp[i][j] be the longest common prefix of A[i:] and B[j:]. Whenever A[i] == B[j], we know dp[i][j] = dp[i+1][j+1] + 1. Also, the answer is max(dp[i][j]) over all i, j.
We can perform bottom-up dynamic programming to find the answer based on this recurrence. Our loop invariant is that the answer is already calculated correctly and stored in dp for any larger i, j.
總結
以上是生活随笔為你收集整理的leetcode 718. Maximum Length of Repeated Subarray | 718. 最长重复子数组(动态规划)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode 115. Distin
- 下一篇: leetcode 310. Minimu