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 bb0af3d commit c3e2c27Copy full SHA for c3e2c27
java/0525-contiguous-array.java
@@ -0,0 +1,28 @@
1
+class Solution {
2
+ public int findMaxLength(int[] nums) {
3
+ int zero = 0;
4
+ int one = 0;
5
+ int res = 0;
6
+
7
+ HashMap<Integer, Integer> diffIndex = new HashMap<>();
8
9
+ for (int i = 0; i < nums.length; i++) {
10
+ int n = nums[i];
11
+ if (n == 0)
12
+ zero++;
13
+ else
14
+ one++;
15
+ if (diffIndex.get(one - zero) == null)
16
+ diffIndex.put(one - zero, i);
17
18
+ if (one == zero) {
19
+ res = one + zero;
20
+ } else {
21
+ int idx = diffIndex.getOrDefault(one - zero, 0);
22
+ res = Math.max(res, i - idx);
23
+ }
24
25
26
+ return res;
27
28
+}
0 commit comments