-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-71.java
More file actions
77 lines (72 loc) · 2.39 KB
/
leet-code-q-71.java
File metadata and controls
77 lines (72 loc) · 2.39 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
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
class Solution {
public int[] processQueries(int n, int[][] connections, int[][] queries) {
n++;
final int[] l = new int[n];
for (int i = 1; i < n; i++) {
l[i] = i;
}
for (int[] as : connections) {
l[getLabel(l, as[0])] = l[getLabel(l, as[1])];
}
final int[] counts = new int[n];
for (int i = 0; i < n; i++) {
counts[getLabel(l, i)]++;
}
updateCounts(counts);
final int[] starts = counts.clone();
final int[] sorted = new int[n];
for (int i = 0; i < n; i++) {
sorted[counts[l[i]]++] = i;
}
final int[] r = new int[queries.length];
int len = 0;
final boolean[] offline = new boolean[n];
for (var q: queries) {
final int x = q[1];
if (q[0] == 1) {
if (offline[x]) {
final int label = l[x];
final int end = counts[label];
int start = starts[label];
while (start < end && offline[sorted[start]]) {
start++;
}
starts[label] = start;
r[len++] = start == end ? -1 : sorted[start];
} else {
r[len++] = x;
}
} else {
offline[x] = true;
}
}
return Arrays.copyOf(r, len);
}
static final int[] arr = new int[100_001];
private static int check(int[] sorted, int[] source, int[] target, int start, int end) {
int r = 0;
for (int i = start; i < end; i++) {
final int idx = sorted[i];
if (++arr[source[idx]] > 0) r++;
if (arr[target[idx]]-- > 0) r--;
}
for (int i = start; i < end; i++) {
final int idx = sorted[i];
arr[source[idx]] = 0;
arr[target[idx]] = 0;
}
return r;
}
private static void updateCounts(int[] count) {
int sum = 0;
for (int r = 0; r < count.length; r++) {
final int newSum = sum + count[r];
count[r] = sum;
sum = newSum;
}
}
static int getLabel(final int[] labels, int idx) {
final int current = labels[idx];
return (current == idx || current < 0)? idx : (labels[idx] = getLabel(labels, current));
}
}