当前位置:网站首页>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
边栏推荐
- Thinking on demand development
- QT writing the Internet of things management platform 38- multiple database support
- 泰山OFFICE技术讲座:关于背景(底纹和高亮)的顺序问题
- 【ISMB2022教程】图表示学习的精准医疗,哈佛大学Marinka Zitnik主讲,附87页ppt
- Kotlin inheritance
- 实战模拟│JWT 登录认证
- 什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
- [problem] Druid reports exception SQL injection violation, part always true condition not allow solution
- C语言-入门-基础-语法-流程控制(七)
- Neural network IOT platform construction (IOT platform construction practical tutorial)
猜你喜欢
ACM组合计数入门
Employment prospects of neural network Internet of things application technology [welcome to add]
Multi table operation - external connection query
Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
In operation (i.e. included in) usage of SSRs filter
实践示例理解js强缓存协商缓存
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
Detailed explanation of Audi EDI invoice message
Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)
随机推荐
实践示例理解js强缓存协商缓存
六石编程学:关于代码,有六个得意
实战模拟│JWT 登录认证
被奉为经典的「金字塔原理」,教给我们哪些PPT写作技巧?
什么是区块哈希竞猜游戏系统开发?哈希竞猜游戏系统开发(案例成熟)
Ziguang zhanrui completed the first 5g R17 IOT NTN satellite on the Internet of things in the world
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
Write it down once Net analysis of thread burst height of an industrial control data acquisition platform
Free soldier
New wizard effect used by BCG
MySQL中的日期时间类型与格式化方式
[QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
更强的 JsonPath 兼容性及性能测试之2022版(Snack3,Fastjson2,jayway.jsonpath)
Introduction to ACM combination counting
凌云出海记 | 沐融科技&华为云:打造非洲金融SaaS解决方案样板
Related concepts of federal learning and motivation (1)
Kotlin classes and objects
实战模拟│JWT 登录认证
QT writing the Internet of things management platform 38- multiple database support
Integritee通过XCM集成至Moonriver,为其生态系统带来企业级隐私解决方案