-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContiguousArray.java
64 lines (58 loc) · 1.84 KB
/
ContiguousArray.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package techQuestions;
import java.util.Collections;
import java.util.HashMap;
import java.util.PriorityQueue;
public class ContiguousArray {
public static void main(String[] args) {
int[] weights = {0,1,0,1};
System.out.println(findMaxLengthMap(weights));
}
public static int findMaxLengthMap(int[] nums) {
HashMap<Integer, Integer> map = new HashMap<>();
// key = count , value = index
int count = 0;
map.put(0, -1);
int maxLength = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == 1) {
count++;
} else {
count--;
}
if (map.containsKey(count)) {
maxLength = Math.max(maxLength, i - map.get(count));
continue;
}
map.put(count, i);
}
return maxLength;
}
public static int findMaxLength(int[] nums) {
// first for loop runs N times where N == nums.length
// second for loop runt N-1 times
int max_length = 0;
for (int i = 0; i < nums.length - 1; i++) {
int zero_count = 0;
int one_count = 0;
if (nums[i] == 1) {
one_count++;
} else {
zero_count++;
}
for (int j = i + 1; j < nums.length; j++) {
if (((zero_count - one_count) > (nums.length - j)) || ((one_count - zero_count) > (nums.length - j))) {
break;
}
if (nums[j] == 1) {
one_count++;
} else {
zero_count++;
}
if (zero_count == one_count) {
max_length = Math.max(max_length, (j - i) + 1);
}
}
}
return max_length;
}
}