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 实现

  1. class Solution {
  2. public int findLength(int[] A, int[] B) {
  3. if (A == null || A.length == 0 || B == null || B.length == 0) {
  4. return 0;
  5. }
  6. int m = A.length, n = B.length;
  7. int res = Integer.MIN_VALUE;
  8. int[][] dp = new int[m + 1][n + 1];
  9. for (int i = m - 1; i >= 0; i--) {
  10. for (int j = n - 1; j >= 0; j--) {
  11. dp[i][j] = A[i] == B[j] ? dp[i + 1][j + 1] + 1 : 0;
  12. res = Math.max(res, dp[i][j]);
  13. }
  14. }
  15. return res;
  16. }
  17. }

相似题目

参考资料

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

  1. [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 ...

  2. Java实现 LeetCode 718 最长重复子数组(动态规划)

    718. 最长重复子数组 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例 1: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出: 3 解释 ...

  3. leetcode 718. 最长重复子数组

    问题描述 给两个整数数组 A 和 B ,返回两个数组中公共的.长度最长的子数组的长度. 示例: 输入: A: [1,2,3,2,1] B: [3,2,1,4,7] 输出:3 解释: 长度最长的公共子数 ...

  4. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  5. 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 ...

  6. [LeetCode]Maximum Length of Repeated Subarray

    Maximum Length of Repeated Subarray: Given two integer arrays A and B, return the maximum length of ...

  7. 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 ...

  8. 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 ...

  9. [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 ...

随机推荐

  1. 《挑战30天C++入门极限》新手入门:C/C++中枚举类型(enum)

        新手入门:C/C++中枚举类型(enum) 如果一个变量你需要几种可能存在的值,那么就可以被定义成为枚举类型.之所以叫枚举就是说将变量或者叫对象可能存在的情况也可以说是可能的值一一例举出来. ...

  2. (4)Go程序结构和流程控制

    Go程序主要由以下几部分组成:(具体可以参考2选择结构中的实例) *包声明 *导入包 *函数 *变量 *语句和表达式 *注释 流程控制 1.顺序结构 2.选择结构  (1)if else if 和 e ...

  3. Note_4.7

    2019/4/7 奇奇怪怪的笔记 狄利克雷卷积  \(μ∗1=ϵ\),莫比乌斯反演 \(Id=φ∗1⇒φ=μ∗Id\) \(d=1∗1⇒1=μ∗d\) \(σ=Id∗1⇒Id=μ∗σ\) \(σ=φ∗ ...

  4. 小程序input组件失焦的使用

    失去焦点就开始做数据请求判断电话号码是正确 <view class='register-input-box'> <input class='register-input' place ...

  5. RIP子网划分及扩展详解

  6. Mysql 查看所有线程,被锁的表等

    ## 查看所有MYSQl相关的线程 > show full processlist; ## 杀死线程id为2的线程 > kill 2 ## 查看服务器状态 > show status ...

  7. ubuntu16.04安装opencv3.4.1教程

    最近opencv3.4.1发布了,想换个新的试试鲜,于是把配置的过程通过博文的方式记录下来,方便查阅. 本教程原为3.3.0,但经过博主亲测,3.4.0.3.4.1皆适用 1.去官网下载opencv, ...

  8. 当fixed元素相互嵌套时,父元素会影响子元素的层叠关系,最好不要嵌套使用fixed

    问题:fixed元素被另一个fixed元素包含的时候在chrome下fixed子元素的定位会受到父元素的影响. 解释:层叠关系是受层叠上下文影响的.文档中的层叠上下文由满足以下任意一个条件的元素形成: ...

  9. Shell Script 入门教程

    和 Shell 的区别 Shell 是一个用 C 语言编写的程序,它是用户使用 Linux 的桥梁. Shell 即是一种命令语言,又是一种程序设计语言. Shell 是指一种应用程序,这个应用程序提 ...

  10. IDEA使用本机指定的java环境

    IDEA使用本机指定的java环境     原文链接:https://my.oschina.net/ElEGenT/blog/3053147 idea 的安装包内有自己的jre. idea 默认使用自 ...