forked from BigEggStudy/LeetCode-CS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0525-ContiguousArray.cs
More file actions
33 lines (29 loc) · 910 Bytes
/
0525-ContiguousArray.cs
File metadata and controls
33 lines (29 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//-----------------------------------------------------------------------------
// Runtime: 216ms
// Memory Usage: 43.6 MB
// Link: https://leetcode.com/submissions/detail/324379199/
//-----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
namespace LeetCode
{
public class _0525_ContiguousArray
{
public int FindMaxLength(int[] nums)
{
var map = new Dictionary<int, int>();
map.Add(0, -1);
var maxlen = 0;
var count = 0;
for (int i = 0; i < nums.Length; i++)
{
count += nums[i] == 1 ? 1 : -1;
if (map.ContainsKey(count))
maxlen = Math.Max(maxlen, i - map[count]);
else
map.Add(count, i);
}
return maxlen;
}
}
}