Maximum Length of Repeated Subarray

2024-09-04 14:18

本文主要是介绍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].

思路:跟Longest Common Substring && Longest continuous Common History 很类似;

dp[i][j] = dp[i - 1][j - 1] + 1; s[i] == s[j]

class Solution {public int findLength(int[] A, int[] B) {if(A == null || B == null) {return 0;}int n = A.length;int m = B.length;int maxlen = 0;int[][] dp = new int[n + 1][m + 1];for(int i = 1; i <= n; i++) {for(int j = 1; j <= m; j++) {if(A[i - 1] == B[j - 1]) {dp[i][j] = dp[i - 1][j - 1] + 1;maxlen = Math.max(maxlen, dp[i][j]);} else {dp[i][j] = 0;}}}return maxlen;}
}

 

这篇关于Maximum Length of Repeated Subarray的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1136216

相关文章

Cannot read property ‘length‘ of null while opening vscode terminal

同一问题地址:Cannot read property ‘length’ of null while opening vscode terminal 问题描述 One day, 我在ubuntu 18.04下用vscode打开一个项目,并想和往常一样在vscode使用终端,发现报错Cannot read property 'length' of null。 解决 打开setting.jso

java中的length与length()与size()

正确用法 Array.length int[] arr = {1,2,3};int x = arr.length;//arr.length = 3 String.length() String s = "123";int x = s.length();//s.length() = 3 Collection.size() ArrayList<Integer> list = n

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

mysql数据库中的字符串长度函数:LENGTH() 与 CHAR_LENGTH()

在数据库管理系统中,处理字符串数据时,了解字符串的长度是一个常见且重要的需求。无论是为了数据验证、格式化输出,还是在进行复杂的查询操作中,准确获取字符串的长度都是必不可少的。SQL标准提供了几种函数来帮助我们实现这一目标,其中LENGTH()和CHAR_LENGTH()是两个常被提及的函数,尽管它们在某些数据库系统中可能表现出相似的行为,但在一些细节上存在差异。本文将深入探讨这两个函数的用法及其区

Maximum likelihood function maximizes what thing?

最大似然函数(Maximum Likelihood Function)最大化的是数据在给定参数下出现的概率。具体来说,它最大化的是似然函数(Likelihood Function),即给定参数 ( \theta ) 下观测数据的概率。在统计学中,似然函数 ( L(\theta) ) 通常定义为所有独立观测数据点概率的乘积,对于参数 ( \theta ) 的函数。 对于一组独立同分布的观测数据

ORA-24067: exceeded maximum number of subscribers for queue ADMIN.SMS_MT_QUEUE

临时处理办法: delete from aq$_ss_MT_tab_D;delete from aq$_ss_MT_tab_g;delete from aq$_ss_MT_tab_h;delete from aq$_ss_MT_tab_i;delete from aq$_ss_MT_tab_p;delete from aq$_ss_MT_tab_s;delete from aq$

[LeetCode] 239. Sliding Window Maximum

题:https://leetcode.com/problems/sliding-window-maximum/description/ 题目 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You

vue3 el-menu 菜单Maximum recursive updates exceeded 报错

vue3 用el-menu实现管理后台左侧菜单,报Uncaught (in promise) Maximum recursive updates exceeded in component <ElMenu>. This means you have a reactive effect that is mutating its own dependencies and thus recursivel

.NetCore+vue3上传图片 Multipart body length limit 16384 exceeded.

实现目标。点击图片上传头像 效果图 前端部分图片上传关键代码 <div class="avatar-wrap"><el-imagestyle="width: 154px; height: 154px":src="form.headPic":fit="fit"/></div><div class="upload-box"><el-uploadclass="avatar-uploader":

whose UTF8 encoding is longer than the max length 32766

问题描述:java.lang.IllegalArgumentException: Document contains at least one immense term in field=“cf_jg.keyword” (whose UTF8 encoding is longer than the max length 32766) 原因:设置为keyword类型的字段,插入很长的大段内容后,报