当前位置:网站首页>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;
}
边栏推荐
猜你喜欢

线性规划例题 投资的收益与风险
![[how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)](/img/ef/1ac272dbd0e5c4d08a8f01f61d334d.png)
[how is the network connected] Chapter 6 requests arrive at the server and respond to the client (end)

简单线性规划问题

【历史上的今天】7 月 2 日:BitTorrent 问世;商业系统 Linspire 被收购;索尼部署 PlayStation Now

Income and risk of linear programming example investment

This "architect growth note" made 300 people successfully change jobs and enter the big factory, with an annual salary of 50W

Use of nexttile function in MATLAB
![[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é)

Modbus协议通信异常

si446使用记录(一):基本资料获取
随机推荐
Navigateur Chrome pour un accès rapide au stackoverflow
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
Are you holding back on the publicity of the salary system for it posts such as testing, development, operation and maintenance?
The difference between class and getClass ()
如何给 SAP Spartacus Storefront 创建新的页面
executescalar mysql_ ExecuteScalar()
Modbus协议通信异常
uniapp H5页面调用微信支付
Solving simple differential equations
RK1126平台项目总结
牛客JS2 文件扩展名
Five reasons to choose SAP Spartacus as the implementation framework of SAP commerce cloud storefront
Eye of depth (II) -- matrix and its basic operations
阿里云子账户 - 权限策略 - 授权给某个账户某个 OSS Bucket 的完全控制权限
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
vector的底层模拟实现
嵌入式 ~ 介绍
Dstat use [easy to understand]
【網絡是怎樣連接的】第六章 請求到達服務器以及響應給客戶端(完結)
Explanation of traceroute command