当前位置:网站首页>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
边栏推荐
- PHP pseudo original API docking method
- How to adapt your games to different sizes of mobile screen
- 实战模拟│JWT 登录认证
- #夏日挑战赛#带你玩转HarmonyOS多端钢琴演奏
- Employment prospects and current situation of Internet of things application technology
- Neural network IOT platform construction (IOT platform construction practical tutorial)
- FS4061A升压8.4V充电IC芯片和FS4061B升压12.6V充电IC芯片规格书datasheet
- repeat_ P1002 [NOIP2002 popularization group] cross the river pawn_ dp
- 栈:如何实现有效括号的判断?
- [QNX Hypervisor 2.2用户手册]6.3.1 工厂页和控制页
猜你喜欢
What should we pay attention to when doing social media marketing? Here is the success secret of shopline sellers!
YOLOv5s-ShuffleNetV2
多表操作-外连接查询
TCP waves twice, have you seen it? What about four handshakes?
一文搞懂Go语言中文件的读写与创建
Detailed explanation of Audi EDI invoice message
精选综述 | 用于白内障分级/分类的机器学习技术
Chrome开发工具:VMxxx文件是什么鬼
C language - Introduction - Foundation - grammar - process control (VII)
Pytoch learning (4)
随机推荐
Qt编写物联网管理平台38-多种数据库支持
公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!
Process of manually encrypt the mass-producing firmware and programming ESP devices
Multi table operation - external connection query
How to adapt your games to different sizes of mobile screen
多表操作-内连接查询
YOLOv5s-ShuffleNetV2
哈希(Hash)竞猜游戏系统开发功能分析及源码
HMM hidden Markov model and code implementation
Introduction to ACM combination counting
如何让你的小游戏适配不同尺寸的手机屏幕
[ismb2022 tutorial] the picture shows the precision medicine of learning. Marinka zitnik, Harvard University, keynote speaker, with 87 ppt
Related concepts of federal learning and motivation (1)
Pointnext: review pointnet through improved model training and scaling strategies++
Basic use of kotlin
Decryption function calculates "task state and lifecycle management" of asynchronous task capability
关于联邦学习和激励的相关概念(1)
Small hair cat Internet of things platform construction and application model
On communication bus arbitration mechanism and network flow control from the perspective of real-time application
Template_ Judging prime_ Square root / six prime method