LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)

718. 最长重复子数组
718. Maximum Length of Repeated Subarray

题目描述
给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。

LeetCode718. Maximum Length of Repeated Subarray

示例:

输入: s = 7, nums = [2,3,1,2,4,3]
输出: 2
解释: 子数组 [4,3] 是该条件下的长度最小的连续子数组。

进阶:
如果你已经完成了 O(n) 时间复杂度的解法,请尝试 O(nlogn) 时间复杂度的解法。

Java 实现

class Solution {
    public int findLength(int[] A, int[] B) {
        if (A == null || A.length == 0 || B == null || B.length == 0) {
            return 0;
        }
        int m = A.length, n = B.length;
        int res = Integer.MIN_VALUE;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = m - 1; i >= 0; i--) {
            for (int j = n - 1; j >= 0; j--) {
                dp[i][j] = A[i] == B[j] ? dp[i + 1][j + 1] + 1 : 0;
                res = Math.max(res, dp[i][j]);
            }
        }
        return res;
    }
}

相似题目

  • 209. 长度最小的子数组

参考资料

原文地址:https://www.cnblogs.com/hglibin/p/10924592.html

时间: 2024-08-02 10:47:44

LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)的相关文章

[Swift]LeetCode718. 最长重复子数组 | Maximum Length of Repeated Subarray

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1].  Note: 1 <= len(A),

[LeetCode] Maximum Length of Repeated Subarray 最长的重复子数组

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= len(A),

[leetcode] 718. Maximum Length of Repeated Subarray

Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= len(A),

718. Maximum Length of Repeated Subarray

#week9 Given two integer arrays A and B, return the maximum length of an subarray that appears in both arrays. Example 1: Input: A: [1,2,3,2,1] B: [3,2,1,4,7] Output: 3 Explanation: The repeated subarray with maximum length is [3, 2, 1]. Note: 1 <= l

LeetCode:最长公共前缀【14】

LeetCode:最长公共前缀[14] 题目描述 编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存

leetcode 673. 最长递增子序列的个数 java

题目: 给定一个未排序的整数数组,找到最长递增子序列的个数. 示例 1: 输入: [1,3,5,4,7]输出: 2解释: 有两个最长递增子序列,分别是 [1, 3, 4, 7] 和[1, 3, 5, 7].示例 2: 输入: [2,2,2,2,2]输出: 5解释: 最长递增子序列的长度是1,并且存在5个子序列的长度为1,因此输出5.注意: 给定的数组长度不超过 2000 并且结果一定是32位有符号整数. 解题: 方法:动态规划 假设对于以 nums[i] 结尾的序列,我们知道最长序列的长度 le

Java中将数组的length计算放在循环体外面可以提升运行速度

示例代码1: 将length计算放在循环体外面,在leetcode运行时,结果只打败了26%的人. public void moveZeroes(int[] nums) { int counter = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != 0) nums[counter++] = nums[i]; } for (int j = counter; j < nums.length; j++) { nums[j] = 0;

最长连续公共子串、最长公共子串(可以非连续)、最长回文串(连续)、最长回文串(可以不连续)、最长递增数组的求解

问题:最长连续公共子串.最长公共子串(可以非连续).最长回文串(连续).最长回文串(可以不连续).最长递增数组.长方形镶嵌最多的求解 方法:上述问题有相似性,都可以采用动态规划进行求解. (1)最长连续公共子串: 如果A[i]==B[j], dp[i][j]=dp[i-1][j-1]+1; 否则,dp[i][j]=0; (2)最长公共子串(可非连续): 如果A[i]==B[j], dp[i][j]=dp[i-1][j-1]+1; 否则,dp[i][j]=dp[i-1][j-1]; (3)最长回文

leetcode 刷题之路 83 Maximum Subarray

Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [?2,1,?3,4,?1,2,1,?5,4], the contiguous subarray [4,?1,2,1] has the largest sum = 6. 输入一个整形数组,数组里有正数也有负数,求所有子数组的和的最大