当前位置:网站首页>最长的可整合子数组的长度
最长的可整合子数组的长度
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)
,符合要求。
更多
边栏推荐
- Kotlin inheritance
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- Pointnext: review pointnet through improved model training and scaling strategies++
- Kotlin condition control
- What financial products can you buy with a deposit of 100000 yuan?
- Cbcgpprogressdlg progress bar used by BCG
- 如何让你的小游戏适配不同尺寸的手机屏幕
- 软件客户端数字签名一定要申请代码签名证书吗?
- 什么叫内卷?
- 一文搞懂Go语言中文件的读写与创建
猜你喜欢
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
记一次 .NET 某工控数据采集平台 线程数 爆高分析
What is the application technology of neural network and Internet of things
一文搞懂Go语言中文件的读写与创建
C语言-入门-基础-语法-流程控制(七)
Related concepts of federal learning and motivation (1)
New wizard effect used by BCG
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
解密函数计算异步任务能力之「任务的状态及生命周期管理」
随机推荐
Anhui Zhong'an online culture and tourism channel launched a series of financial media products of "follow the small editor to visit Anhui"
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
Detailed explanation of Audi EDI invoice message
Six stones programming: about code, there are six triumphs
凌云出海记 | 文华在线&华为云:打造非洲智慧教学新方案
Template_ Large integer subtraction_ Regardless of size
【深度学习】一文看尽Pytorch之十九种损失函数
泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
解密函数计算异步任务能力之「任务的状态及生命周期管理」
Informatics Olympiad 1336: [example 3-1] find roots and children
[problem] Druid reports exception SQL injection violation, part always true condition not allow solution
Thinking on demand development
Creation of JVM family objects
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
【历史上的今天】7 月 4 日:第一本电子书问世;磁条卡的发明者出生;掌上电脑先驱诞生
Multi table operation inner join query
多表操作-外连接查询
15million employees are easy to manage, and the cloud native database gaussdb makes HR office more efficient
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Process of manually encrypt the mass-producing firmware and programming ESP devices