![https://leetcode.com/problems/crawler-log-folder/]
The Leetcode file system keeps a log each time some user performs a change folder operation.
The operations are described below:
- “../” : Move to the parent folder of the current folder. (If you are already in the main folder, remain in the same folder).
- “./” : Remain in the same folder.
- “x/” : Move to the child folder named x (This folder is guaranteed to always exist).
You are given a list of strings logs where logs[i] is the operation performed by the user at the ith step.
The file system starts in the main folder, then the operations in logs are performed.
Return the minimum number of operations needed to go back to the main folder after the change folder operations.

1
2
3
4
5
| Example 1:
Input: logs = ["d1/","d2/","../","d21/","./"]
Output: 2
Explanation: Use this change folder operation "../" 2 times and go back to the main folder.
|

1
2
3
4
5
6
7
8
9
| Example 2:
Input: logs = ["d1/","d2/","./","d3/","../","d31/"]
Output: 3
Example 3:
Input: logs = ["d1/","../","../","../"]
Output: 0
|
Constraints:
- 1 <= logs.length <= 103
- 2 <= logs[i].length <= 10
- logs[i] contains lowercase English letters, digits, ‘.’, and ‘/’.
- logs[i] follows the format described in the statement.
- Folder names consist of lowercase English letters and digits.
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
| class Solution {
public int makeConnected(int n, int[][] connections) {
UF uf = new UF(n);
int extra = 0;
for (int i = 0; i < connections.length; i++) {
int a = connections[i][0];
int b = connections[i][1];
if (uf.union(a, b)) extra++;
}
int group = 0;
for (int i = 0; i < n; i++) {
if (uf.find(i) == i) {
group++;
}
}
return extra >= group - 1 ? group - 1 : -1;
}
class UF {
private int[] a;
UF(int n) {
this.a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i;
}
}
boolean union(int p, int q) {
int pid = find(p);
int qid = find(q);
if (pid == qid) {
return true;
}
a[qid] = pid;
return false;
}
int find(int p) {
while (p != a[p]) {
a[p] = a[a[p]];
p = a[p];
}
return p;
}
}
}
class Solution2 {
public int makeConnected(int n, int[][] connections) {
if (connections.length < n - 1) {
return -1;
}
UF uf = new UF(n);
for (int i = 0; i < connections.length; i++) {
int a = connections[i][0];
int b = connections[i][1];
uf.union(a, b);
}
Set<Integer> componentCount = new HashSet<>();
int free = 0;
for (int i = 0; i < n; i++) {
int id = uf.find(i);
if (componentCount.add(id)) {
free = free + uf.edges[id] - (uf.size(id) - 1);
}
}
if (componentCount.size() == 1) {
return 0;
}
int needEdges = componentCount.size() - 1;
if (free >= needEdges) {
return needEdges;
} else {
return 1;
}
}
class UF {
private int[] a;
private int[] sz;
private int[] edges;
UF(int n) {
this.a = new int[n];
this.sz = new int[n];
this.edges = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i;
sz[i] = 1;
}
}
void union(int p, int q) {
int pid = find(p);
int qid = find(q);
if (pid == qid) {
edges[qid]++;
return;
}
if (sz[pid] > sz[qid]) {
a[qid] = pid;
sz[pid] += sz[qid];
edges[pid] += edges[qid] + 1;
} else {
a[pid] = qid;
sz[qid] += sz[pid];
edges[qid] += edges[pid] + 1;
}
}
int find(int p) {
while (p != a[p]) {
a[p] = a[a[p]];
p = a[p];
}
return p;
}
int size(int p) {
return sz[find(p)];
}
}
}
|