当前位置:网站首页>最长的可整合子数组的长度
最长的可整合子数组的长度
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),符合要求。
更多
边栏推荐
- Abc229 summary (connected component count of the longest continuous character graph in the interval)
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
- [Beijing Xunwei] i.mx6ull development board porting Debian file system
- 【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
- Cbcgpprogressdlg progress bar used by BCG
- 华为云云商店首页 Banner 资源位申请
- Data set division
- 原来这才是 BGP 协议
- Creation of JVM family objects
猜你喜欢

Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company

水晶光电:长安深蓝SL03的AR-HUD产品由公司供应

Dynamic memory management

C server log module

ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声

In the first month of its launch, the tourist praise rate of this campsite was as high as 99.9%! How did he do it?

What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!

公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!

Process of manually encrypt the mass-producing firmware and programming ESP devices

Decryption function calculates "task state and lifecycle management" of asynchronous task capability
随机推荐
How is the entered query SQL statement executed?
C server log module
Abc229 summary (connected component count of the longest continuous character graph in the interval)
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
Detailed explanation of Audi EDI invoice message
Related concepts of federal learning and motivation (1)
Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
TCP waves twice, have you seen it? What about four handshakes?
Utilisation de la barre de progression cbcggprogressdlgctrl utilisée par BCG
Niuke Xiaobai month race 7 who is the divine Archer
解密函数计算异步任务能力之「任务的状态及生命周期管理」
实战模拟│JWT 登录认证
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
c# . Net MVC uses Baidu ueditor rich text box to upload files (pictures, videos, etc.)
HMM hidden Markov model and code implementation
C # use stopwatch to measure the running time of the program
多表操作-外连接查询