We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a5d4ff2 commit bdc14aeCopy full SHA for bdc14ae
May-LeetCoding-Challenge/26-Contiguous-Array/Contiguous-Array.cpp
@@ -0,0 +1,19 @@
1
+class Solution {
2
+public:
3
+ int findMaxLength(vector<int>& nums) {
4
+ int n = nums.size();
5
+ int sum = 0;
6
+ map<int, int> min_index;
7
+
8
+ int ans = 0;
9
+ for (int i = 0; i < n; i++){
10
+ sum += (nums[i] == 0)?-1:1;
11
+ if (sum == 0) ans = i+1;
12
+ else if (min_index.find(sum) == min_index.end())
13
+ min_index[sum] = i;
14
+ else ans = max(ans, i - min_index[sum]);
15
+ }
16
+ return ans;
17
18
+};
19
0 commit comments