当前位置:网站首页>Length of the longest integrable subarray
Length of the longest integrable subarray
2022-07-04 20:32:00 【GreyZeng】
The length of the longest integrable subarray
author :Grey
Original address : The length of the longest integrable subarray
Topic link
Cattle guest : The length of the longest integrable subarray
describe
First give the definition of integrable array : If an array is sorted , The absolute value of the difference between every two adjacent numbers is 1, Or the array length is 1, Then the array is an integrable array . for example ,
[5, 3, 4, 6, 2]
The order is[2, 3, 4, 5, 6]
, The absolute value conforming to the difference between every two adjacent numbers is 1, So this array is an integrable array , Given an arrayarr
, Please return the length of the largest integrable subarray .
for example ,[5, 5, 3, 2, 6, 4, 3]
The maximum integrable subarray of is[5, 3, 2, 6, 4]
, So please return to 5
Data range :0 <n≤100000
, Each number in the array satisfies 0≤val≤10^9
requirement : The time complexity is O(n^2)
, The space complexity is O(n)
Advanced : Time complexity O(nlogn)
, Spatial complexity O(n)
Violence solution
Enumerate each subarray , Then sort each sub array , Then determine whether it is an integrable array , The complete code is as follows
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;
}
The time complexity of violent solution is O(N^3*logN)
, Obviously timeout. .
Optimize the solution
If an array belongs to an integrable array , Then this array must meet the following two conditions :
The first condition : There are no duplicate elements in the array .
The second condition : The difference between the maximum and minimum values in the array is equal to Number of array elements -1
.
If the above two conditions are satisfied , It must be an integrable array .
We can set up a HashSet
To determine whether the element is repeated .
Complete code
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;
}
The time complexity of the whole algorithm O(N*logN)
, Meet the requirements .
more
边栏推荐
- Kotlin cycle control
- Pointnet / pointnet++ point cloud data set processing and training
- 更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
- Qt编写物联网管理平台38-多种数据库支持
- 1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
- B2B mall system development of electronic components: an example of enabling enterprises to build standardized purchase, sale and inventory processes
- Thinking on demand development
- 2022 version of stronger jsonpath compatibility and performance test (snack3, fastjson2, jayway.jsonpath)
- New wizard effect used by BCG
- CDGA|数据治理不得不坚持的六个原则
猜你喜欢
多表操作-外连接查询
Selected review | machine learning technology for Cataract Classification / classification
Actual combat simulation │ JWT login authentication
应用实践 | 蜀海供应链基于 Apache Doris 的数据中台建设
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
Regular replacement [JS, regular expression]
多表操作-内连接查询
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
c# .net mvc 使用百度Ueditor富文本框上传文件(图片,视频等)
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
随机推荐
C语言-入门-基础-语法-流程控制(七)
Template_ Judging prime_ Square root / six prime method
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
QT writing the Internet of things management platform 38- multiple database support
ICML 2022 | Meta提出鲁棒的多目标贝叶斯优化方法,有效应对输入噪声
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
Development and construction of DFI ecological NFT mobile mining system
C # use stopwatch to measure the running time of the program
NetCore3.1 Json web token 中间件
FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
关于联邦学习和激励的相关概念(1)
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
How is the entered query SQL statement executed?
【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
[Beijing Xunwei] i.mx6ull development board porting Debian file system
MySQL中的日期时间类型与格式化方式
Neural network IOT platform construction (IOT platform construction practical tutorial)
太方便了,钉钉上就可完成代码发布审批啦!