当前位置:网站首页>Longest non repeating subarray
Longest non repeating subarray
2022-07-02 17:43:00 【It's a little rabbit!】
Longest non repeating subarray
The main idea of the topic
Given a length of n n n Array of a r r arr arr , return a r r arr arr The length of the longest non repeating element subarray of , No repetition means that all numbers are different .
Subarray is continuous , such as [1,3,5,7,9] The subarrays of are [1,3],[3,5,7] wait , however [1,3,7] It's not a subarray
Data range
0 ≤ a r r . l e n g t h ≤ 1 0 5 0≤arr.length≤10^5 0≤arr.length≤105
0 < a r r [ i ] ≤ 1 0 5 0< arr[i] ≤ 10^5 0<arr[i]≤105
Examples
Input : [2,3,4,5]
Return value :4
explain :[2,3,4,5] Is the longest subarray
solution + prove
The topic needs to find the longest non repeating sub array , First of all, make it clear , Subarray must be contiguous
First of all, make it clear : The largest scale data of this problem can reach 2 e 5 2e5 2e5 Level , If double circulation is adopted , Then it will time out ( The final exam passed because the data was too weak )
Just a quick introduction : A normal game machine , 1 s 1s 1s You can run 1 e 7 − 1 e 8 1e7-1e8 1e7−1e8 The data of , So if it's a double cycle , Can be achieved in this topic 4 e 10 4e10 4e10 Amount of computation , So it's going to be overtime
Then we need a faster algorithm , To solve this problem
// One idea given by the teacher is dynamic planning , But since I didn't take this idea to solve , So this kind of thinking is not for the time being ( I don't want to be lazy ~╭(╯^╰)╮)
So how do we do that ?
A very simple idea : If all the numbers , Only once , Then the answer is length
Except in this case , We also need to explore the nature of the topic : Be careful. : A longest non repeating subarray , For this subarray , Each element is continuous in position and only appears once , let me put it another way , This array cannot be made larger
Simple proof : If this array can add an element on the left , Obviously, the new array with new elements is longer than the original array 1, Contradict the original assumption , Adding an element on the right is similar .
therefore , You can get one Important conclusions : For this subarray of answers , The first element on its left , Or the first element to the right , It must have appeared in the subarray and only once
So what did we think of ? you 're right , Namely Count
Give a c n t cnt cnt Array , This array is used to record array elements a [ i ] a[i] a[i] In a definite interval [ l e f t , r i g h t ] [left,right] [left,right] Number of occurrences in
Next , We just need to maintain [ l e f t , r i g h t ] [left,right] [left,right] This interval , Ensure that all numbers in the interval appear and only appear once , Specific to the method , That is, we adopt Double pointer , At the left end point l e f t left left after , Every time you move the right endpoint , Simultaneous counting , If the newly added number leads to the current range [ l e f t , r i g h t ] [left,right] [left,right] The number of occurrences of a certain number in is greater than 1, Then we keep moving the left endpoint , Make all numbers in the whole interval appear and only appear once .
Last , It's important to point out that , Although we adopt a double cycle in form , But in essence, the upper limit of this code is 2 n 2n 2n So there's no overtime
Standard range
Double pointer + Count
/** * * @param arr int Integer one-dimensional array the array * @param arrLen int arr The length of the array * @return int integer * * C Language declaration defines global variables. Please add static, Prevent duplicate definitions * * C Language declaration defines global variables. Please add static, Prevent duplicate definitions */
#define N 200005
static int cnt[N];
int maxLength(int* arr, int arrLen ) {
// Special judgement
if (arrLen < 2) return arrLen;
int maxLen = 0;
// Double pointer , Move the right border
int left = 0,right = 0;
while(right < arrLen)
{
// Count first
cnt[arr[right]]++;
// The new count causes a number in the interval to appear more than 1
while(cnt[arr[right]] > 1)
{
// Start on the left , Delete the numbers until all the numbers in the interval appear and only appear once
cnt[arr[left]]--;
left++;
}
// After processing , The right range +1, Ready for the next cycle
right++;
// Update answers
maxLen = maxLen > (right - left) ? maxLen : (right - left);
}
return maxLen;
}
边栏推荐
- 牛客 JS3 分隔符
- 什么是软件开发中的 green field 和 brown field 模式 - 绿地开发和棕地开发
- 每日一题——“水仙花数”
- Chapter 15 string localization and message Dictionary (1)
- Daily question - xiaolele changes the number
- Nexus Introduction and Xiaobai use idea Packaging and Upload to Nexus 3 private service detailed tutoriel
- class和getClass()的区别
- What are the green field and brown field models in software development - green field development and brown field development
- Wechat applet - arrows floating up and down
- Update iteration of cloud communication interface -- the official launch of subail API V4
猜你喜欢
HBuilderX运行到手机或模拟器提示没有找到设备
Si446 usage record (II): generate header files using wds3
Si446 usage record (I): basic data acquisition
Daily question - inverted string
Platform management background and merchant menu resource management: merchant role management design
Eth data set download and related problems
Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture
[how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)
Virtual lab basic experiment tutorial -7 Polarization (1)
【网络是怎样连接的】第六章 请求到达服务器以及响应给客户端(完结)
随机推荐
RK1126平台项目总结
What are the green field and brown field models in software development - green field development and brown field development
About me
Introduce the scrollintoview() method attribute in detail
executescalar mysql_ ExecuteScalar()
ROS knowledge point - message_filters
When the industrial Internet began to enter the deep-water area, it appeared more in the form of industry
SAP commerce Cloud Architecture Overview
The difference between class and getClass ()
freemarker+poi实现动态生成excel文件及解析excel文件
Virtual lab basic experiment tutorial -7 Polarization (2)
JDBC
HDU - 1114 Piggy-Bank(完全背包)
Making tutorial of chicken feet with pickled peppers
牛客JS2 文件扩展名
Dstat use [easy to understand]
Platform management background and merchant menu resource management: merchant role management design
From collection to output: inventory those powerful knowledge management tools - inventory of excellent note taking software (4)
Chmod command principle and usage details [easy to understand]
PCL knowledge points - voxelized grid method for down sampling of point clouds