当前位置:网站首页>最长的可整合子数组的长度
最长的可整合子数组的长度
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),符合要求。
更多
边栏推荐
- NLP、视觉、芯片...AI重点方向发展几何?青源会展望报告发布[附下载]
- How is the entered query SQL statement executed?
- In operation (i.e. included in) usage of SSRs filter
- 太方便了,钉钉上就可完成代码发布审批啦!
- Is it necessary to apply for code signing certificate for software client digital signature?
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- 什么叫内卷?
- 如何让你的小游戏适配不同尺寸的手机屏幕
- C language - Introduction - Foundation - grammar - process control (VII)
- FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
猜你喜欢

Optimization cases of complex factor calculation: deep imbalance, buying and selling pressure index, volatility calculation

Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world

FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC

Actual combat simulation │ JWT login authentication

Qt编写物联网管理平台38-多种数据库支持

Abc229 summary (connected component count of the longest continuous character graph in the interval)

C language - Introduction - Foundation - grammar - process control (VII)

Siemens HMI download prompts lack of panel image solution

【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt

Related concepts of federal learning and motivation (1)
随机推荐
输入的查询SQL语句,是如何执行的?
华为nova 10系列支持应用安全检测功能 筑牢手机安全防火墙
Cbcgpprogressdlg progress bar used by BCG
1005 spell it right (20 points) (pat a)
Chrome development tool: what the hell is vmxxx file
In operation (i.e. included in) usage of SSRs filter
什么叫内卷?
How is the entered query SQL statement executed?
[QNX hypervisor 2.2 user manual]6.3.1 factory page and control page
针对深度学习的“失忆症”,科学家提出基于相似性加权交错学习,登上PNAS
Employment prospects and current situation of Internet of things application technology
实践示例理解js强缓存协商缓存
Thinking on demand development
Small hair cat Internet of things platform construction and application model
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
Key rendering paths for performance optimization
kotlin 继承
需求开发思考
精选综述 | 用于白内障分级/分类的机器学习技术