-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContiguousBinaryArray.java
More file actions
43 lines (33 loc) · 1.34 KB
/
ContiguousBinaryArray.java
File metadata and controls
43 lines (33 loc) · 1.34 KB
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
34
35
36
37
38
39
40
41
42
43
package Algorithms.PrefixSum;
import java.util.HashMap;
import java.util.Map;
/**<pre>
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 25 Sept 2024,
*/
public class ContiguousBinaryArray {
public static void main(String[] args) {
System.out.println(findMaxLength(new int[]{0,1,0,0,1,1,1})); // it is little similar to Longest Valid Parentheses ")()())"
}
// prefixSum Hashmap approach
public static int findMaxLength(int[] nums) {
int prefixSum = 0;
int maxLength = 0;
Map<Integer, Integer> sumsMap = new HashMap<>();
// sumsMap.put(null, null)
for (int i = 0; i < nums.length; i++) {
int num = nums[i] == 0 ? -1 : nums[i];
prefixSum += num;
if(prefixSum == 0 && maxLength < i+1){
maxLength = i+1; // +1 as i is index
}
if(sumsMap.containsKey(prefixSum) && i - sumsMap.get(prefixSum) > maxLength){ // current sum is in map
maxLength = i - sumsMap.get(prefixSum);
}
if(!sumsMap.containsKey(prefixSum)) // or map.putIfAbsent
sumsMap.put(prefixSum, i);
}
return maxLength;
}
// we can also use zerosCount and onesCount approach and check zerosCount == onesCount but research on start index like zerosCount - onesCount == 2
}