-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathCountCompleteSubarrays.java
91 lines (79 loc) · 2.56 KB
/
CountCompleteSubarrays.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package slidingwindow.problems;
import utils.Reader;
import utils.Helper;
import java.util.HashSet;
import java.io.IOException;
// Problem Link: https://leetcode.com/problems/count-complete-subarrays-in-an-array/description/
class CountCompleteSubarrays {
public static void main (String[] args) throws IOException {
Reader reader = new Reader();
int n = reader.nextInt();
int[] nums = Helper.getIntArray(n, reader);
CountCompleteSubarrays obj = new CountCompleteSubarrays();
System.out.println(obj.countCompleteSubarrays(nums));
}
// O(N) approach through Sliding Window.
public int countCompleteSubarrays(int[] nums) {
int n = nums.length;
HashSet<Integer> distinctElements = new HashSet<>();
for (int i=0; i<n; i++) {
if (!distinctElements.contains(nums[i])) {
distinctElements.add(nums[i]);
}
}
int totalDistinct = distinctElements.size();
int i = 0;
int j = 0;
int[] count = new int[2001];
count[nums[i]]++;
int curDistinct = 1;
int subArrays = 0;
while (true) {
if (curDistinct < totalDistinct) {
j++;
if (j >= n) {
break;
}
if (count[nums[j]] == 0) {
curDistinct++;
}
count[nums[j]]++;
} else {
subArrays += (n-j);
if (i >= n) {
break;
}
count[nums[i]]--;
if (count[nums[i]] == 0) {
curDistinct--;
}
i++;
}
}
return subArrays;
}
// O(N^2) approach through Hashtables.
public int countCompleteSubarraysNaive(int[] nums) {
int n = nums.length;
HashSet<Integer> distinctElements = new HashSet<>();
for (int i=0; i<n; i++) {
if (!distinctElements.contains(nums[i])) {
distinctElements.add(nums[i]);
}
}
int subArrays = 0;
for (int i=0; i<n; i++) {
HashSet<Integer> curDistinct = new HashSet<>();
for (int j=i; j<n; j++) {
if (!curDistinct.contains(nums[j])) {
curDistinct.add(nums[j]);
}
if (curDistinct.size() == distinctElements.size()) {
subArrays += (n-j);
break;
}
}
}
return subArrays;
}
}