当前位置:网站首页>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;
}
边栏推荐
- Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
- JDBC
- Virtual lab basic experiment tutorial -7 Polarization (1)
- dstat使用[通俗易懂]
- IDEA2021.1 安装教程
- 牛客 JS3 分隔符
- 如何给 SAP Spartacus Storefront 创建新的页面
- 维护万星开源向量数据库是什么体验
- Platform management background and merchant menu resource management: merchant role management design
- Explanation of traceroute command
猜你喜欢

Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)

Map集合详细讲解

Platform management background and merchant menu resource management: merchant role management design

Modbus协议通信异常

简单线性规划问题
![[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)](/img/ef/1ac272dbd0e5c4d08a8f01f61d334d.png)
[comment le réseau se connecte] chapitre 6: demande d'accès au serveur et réponse au client (terminé)

Chrome browser quick access stackoverflow

VirtualLab基础实验教程-7.偏振(2)

Si446 usage record (II): generate header files using wds3

The difference of message mechanism between MFC and QT
随机推荐
Microservice architecture practice: Construction of highly available distributed file system fastdfs architecture
Chrome browser quick access stackoverflow
[how to connect the network] Chapter 5 explore the server
Si446 usage record (II): generate header files using wds3
[web technology] 1233 seconds understand web component
class和getClass()的区别
Idea2021.1 installation tutorial
How to create a new page for SAP Spartacus storefront
[how is the network connected] Chapter 4 explores access networks and network operators
uva1169
TCP congestion control details | 2 background
Daily question - xiaolele changes the number
[非线性控制理论]7_High gain and High Frequency
Virtual lab basic experiment tutorial -7 Polarization (1)
Easyswoole3.2 restart failed
chrome瀏覽器快速訪問stackoverflow
SAP commerce Cloud Architecture Overview
JS20 array flattening
Migrate your accelerator based SAP commerce cloud storefront to Spartacus
Platform management background and merchant menu resource management: merchant role management design