当前位置:网站首页>最长的可整合子数组的长度
最长的可整合子数组的长度
2022-07-04 18:48:00 【GreyZeng】
最长的可整合子数组的长度
作者:Grey
原文地址: 最长的可整合子数组的长度
题目链接
描述
先给出可整合数组的定义:如果一个数组在排序之后,每相邻两个数的差的绝对值都为1,或者该数组长度为1,则该数组为可整合数组。例如,
[5, 3, 4, 6, 2]排序后为[2, 3, 4, 5, 6],符合每相邻两个数差的绝对值都为1,所以这个数组为可整合数组,给定一个数组arr, 请返回其中最大可整合子数组的长度。
例如,[5, 5, 3, 2, 6, 4, 3]的最大可整合子数组为[5, 3, 2, 6, 4],所以请返回5
数据范围:0 <n≤100000,数组中每个数都满足0≤val≤10^9
要求:时间复杂度为O(n^2),空间复杂度为O(n)
进阶:时间复杂度O(nlogn),空间复杂度O(n)
暴力解法
枚举每个子数组,然后对每个子数组进行排序,然后判断是否为可整合数组,完整代码如下
public static int getLIL1(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int len = 0;
// O(N^3 * log N)
for (int start = 0; start < arr.length; start++) {
for (int end = start; end < arr.length; end++) {
if (isIntegrated(arr, start, end)) {
len = Math.max(len, end - start + 1);
}
}
}
return len;
}
public static boolean isIntegrated(int[] arr, int left, int right) {
int[] newArr = Arrays.copyOfRange(arr, left, right + 1); // O(N)
Arrays.sort(newArr); // O(N*logN)
for (int i = 1; i < newArr.length; i++) {
if (newArr[i - 1] != newArr[i] - 1) {
return false;
}
}
return true;
}
暴力解法的时间复杂度是O(N^3*logN),显然超时。
优化解
某个数组如果属于可整合数组,则这个数组一定符合如下两个条件:
第一个条件:数组中的元素没有重复。
第二个条件:数组中的最大值和最小值的差值等于数组元素个数-1。
如果满足上述两个条件,一定是可整合数组。
我们可以通过设置一个HashSet来判断元素是否重复。
完整代码
public static int getLIL2(int[] arr) {
if (arr == null || arr.length == 0) {
return 0;
}
int len = 0;
int max = 0;
int min = 0;
HashSet<Integer> set = new HashSet<Integer>();
for (int l = 0; l < arr.length; l++) {
set.clear();
max = arr[l];
min = arr[l];
for (int r = l; r < arr.length; r++) {
if (set.contains(arr[r])) {
break;
}
set.add(arr[r]);
max = Math.max(max, arr[r]);
min = Math.min(min, arr[r]);
if (max - min == r - l) {
len = Math.max(len, r - l + 1);
}
}
}
return len;
}
整个算法时间复杂度O(N*logN),符合要求。
更多
边栏推荐
- Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
- What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
- Selected review | machine learning technology for Cataract Classification / classification
- 九齐单片机NY8B062D单按键控制4种LED状态
- 记一次 .NET 某工控数据采集平台 线程数 爆高分析
- 泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
- Free soldier
- How to adapt your games to different sizes of mobile screen
- NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
- QT writing the Internet of things management platform 38- multiple database support
猜你喜欢

输入的查询SQL语句,是如何执行的?

Multi table operation - external connection query

精选综述 | 用于白内障分级/分类的机器学习技术

Application practice | Shuhai supply chain construction of data center based on Apache Doris

How to adapt your games to different sizes of mobile screen

【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生

应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设

C language - Introduction - Foundation - grammar - process control (VII)
![NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]

Huawei Nova 10 series supports the application security detection function to build a strong mobile security firewall
随机推荐
Kotlin classes and objects
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
Template_ Large integer subtraction_ Regardless of size
Niuke Xiaobai month race 7 who is the divine Archer
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
Niuke Xiaobai month race 7 e applese's super ability
C server log module
What is involution?
Development and construction of DFI ecological NFT mobile mining system
一文搞懂Go语言中文件的读写与创建
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
需求开发思考
被奉为经典的「金字塔原理」,教给我们哪些PPT写作技巧?
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
Free soldier
Six stones programming: about code, there are six triumphs
实战模拟│JWT 登录认证