LeetCode 718. 最长重复子数组(Maximum Length of Repeated Subarray)
718. 最长重复子数组
718. Maximum Length of Repeated Subarray
题目描述
给定一个含有 n 个正整数的数组和一个正整数 s,找出该数组中满足其和 ≥ s 的长度最小的连续子数组。如果不存在符合条件的连续子数组,返回 0。
LeetCode718. Maximum Length of Repeated Subarray
示例:
输出: 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;
}
}
相似题目
参考资料
- https://leetcode.com/problems/maximum-length-of-repeated-subarray/
- https://leetcode-cn.com/problems/maximum-length-of-repeated-subarray/
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 arra ...
- Java实现 LeetCode 718 最长重复子数组(动态规划)
718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...
- leetcode 718. 最长重复子数组
问题描述 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出:3 解释: 长度最长的公共子数 ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- Week 7 - 714. Best Time to Buy and Sell Stock with Transaction Fee & 718. Maximum Length of Repeated Subarray
714. Best Time to Buy and Sell Stock with Transaction Fee - Medium Your are given an array of intege ...
- [LeetCode]Maximum Length of Repeated Subarray
Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...
- 718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- LC 718. Maximum Length of Repeated Subarray
Given two integer arrays A and B, return the maximum length of an subarray that appears in both arra ...
- [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 arra ...
随机推荐
- C语言实现多线程排序
#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <string.h& ...
- python中的zip函数的使用
>>> x = [, , ] >>> y = [, , ] >>> z = [, , ] >>> xyz = list(zip( ...
- Codeforces Round #576 (div.1 + div.2)
Div2 A 长度为\(n(n≤10^5)\)的数组,每个元素不同,求有多少个位置\(d\)满足\(d - x \le j < d \And d < j \le d + y a_d< ...
- Leetcode42. 接雨水
42. 接雨水 做法 考虑单独一列能生产多少贡献:用左右最大值去接这列的水 \(O(n)\) Code class Solution { public: int mx[1000000],rx[1000 ...
- WIN7在计算机管理中修改用户名之后 文件夹名字不变怎么办?
WIN7在计算机管理中修改用户名之后 文件夹名字不变? 可以新建一个管理员账户,起名字,然后删除旧的账户.保留原用户文件夹中的内容,以备不时之需.不要在控制面板的用户账户中修改用户名,修改之后 ...
- postgresql 臭氧8小时聚合函数
1.定义数据拼接函数 CREATE OR REPLACE FUNCTION "public"."sfun"("results" _numer ...
- Spring cloud微服务安全实战-6-5jwt改造之日志及错误处理(1)
在代码里,我们没有认证或者授权的filter.认证和授权的工作现在基本上完全由Spring Security的过滤器接管了. 本节就来看下 如何在Spring Security的过滤器链上加入我们自己 ...
- Spring cloud微服务安全实战-5-7实现基于session的SSO(客户端应用的Session有效期)
授权模式改造成了Authorization code完成了改造的同时也实现了SSO.微服务环境下的前后端分离的单点登陆. 把admin的服务重启.刷新页面 并没有让我去登陆,直接就进入了首页. ord ...
- Spring cloud微服务安全实战-3-7API安全机制之数据加密
这一节来聊一下密码的加密. 加密盐,为了避免两个相同的面加密出来的密文是一样的,每个人的盐不一样, 首先引入工具包,lambdaworks <!-- https://mvnrepository. ...
- 算法习题---5-8图书管理系统*****<双向迭代器>(UVa230)
一:题目 就是输入一系列书本名和作者名,然后输入命令模拟借书和还书,再输出归还的书的摆放位置.要求有两点: 需要对归还的书做特殊排序处理:作者名相同,则书本按书名从小到大排序:否则书本按作者名大小排序 ...