Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a84b648

Browse files
16-03-2024
1 parent 32bf9f7 commit a84b648

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

‎2024/03 - March/16-03-2024.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
3+
Author : Manas Rawat
4+
Date : 16/03/2024
5+
Problem : Contiguous Array
6+
Difficulty : Medium
7+
Problem Link : https://leetcode.com/problems/contiguous-array/description/
8+
Video Solution : NA
9+
10+
*/
11+
12+
13+
class Solution {
14+
public:
15+
int findMaxLength(vector<int>& nums) {
16+
int n = nums.size();
17+
vector<int> pre(n);
18+
19+
pre[0] = nums[0] == 1 ? 1 : -1;
20+
21+
for(int i = 1; i < n; i++){
22+
pre[i] = pre[i - 1];
23+
pre[i] += nums[i] == 1 ? 1 : -1;
24+
}
25+
26+
unordered_map<int, int> first;
27+
first[0] = -1;
28+
int ans = 0;
29+
for(int i = 0; i < n; i++){
30+
if(first.find(pre[i]) != first.end())
31+
ans = max(ans, i - first[pre[i]]);
32+
else
33+
first[pre[i]] = i;
34+
}
35+
36+
return ans;
37+
}
38+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /