当前位置:网站首页>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
边栏推荐
- Installation and use of VMware Tools and open VM tools: solve the problems of incomplete screen and unable to transfer files of virtual machines
- CANN算子:利用迭代器高效实现Tensor数据切割分块处理
- 太方便了,钉钉上就可完成代码发布审批啦!
- C语言-入门-基础-语法-流程控制(七)
- Prometheus installation
- 1009 product of polynomials (25 points) (PAT class a)
- Process of manually encrypt the mass-producing firmware and programming ESP devices
- 凌云出海记 | 一零跃动&华为云:共助非洲普惠金融服务
- Crystal optoelectronics: ar-hud products of Chang'an dark blue sl03 are supplied by the company
- Swagger suddenly went crazy
猜你喜欢

C # use stopwatch to measure the running time of the program

C # better operation mongodb database

多表操作-外连接查询

精选综述 | 用于白内障分级/分类的机器学习技术

Cbcgptabwnd control used by BCG (equivalent to MFC TabControl)

FS8B711S14电动红酒开瓶器单片机IC方案开发专用集成IC
![NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]](/img/79/82763392e74d102921b4e8e601d4c6.png)
NLP, vision, chip What is the development direction of AI? Release of the outlook report of Qingyuan Association [download attached]

多表操作-内连接查询
node强缓存和协商缓存实战示例

实战模拟│JWT 登录认证
随机推荐
1009 product of polynomials (25 points) (PAT class a)
C server log module
[graduation season] green ant new fermented grains wine, red mud small stove. If it snows late, can you drink a cup?
Introduction to ACM combination counting
Pointnet / pointnet++ point cloud data set processing and training
栈:如何实现有效括号的判断?
做社交媒体营销应该注意些什么?Shopline卖家的成功秘笈在这里!
紫光展锐完成全球首个 5G R17 IoT NTN 卫星物联网上星实测
PHP pseudo original API docking method
Aiming at the "amnesia" of deep learning, scientists proposed that based on similarity weighted interleaved learning, they can board PNAS
#夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
What ppt writing skills does the classic "pyramid principle" teach us?
为什么最大速度是光速
Pytoch learning (4)
上线首月,这家露营地游客好评率高达99.9%!他是怎么做到的?
Key rendering paths for performance optimization
太方便了,钉钉上就可完成代码发布审批啦!
Is it necessary to apply for code signing certificate for software client digital signature?
Oracle database, numbers Force 2 decimal places to display-Alibaba Cloud
Selected review | machine learning technology for Cataract Classification / classification