-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily130.cpp
More file actions
172 lines (137 loc) · 5.48 KB
/
Copy pathdaily130.cpp
File metadata and controls
172 lines (137 loc) · 5.48 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <vector>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
// Solution 1 - FAIL
class Solution {
public:
std::vector<std::string> removeSubfolders(std::vector<std::string>& folder) {
/*
edit vector in place?
use seen array/set
if current folder is not in set
add to set
else
idk
translate into graph structure
find longest connection of nodes with only one child (for each tree)
for each root node of tree in forest
traverse until a split in the path is found
if longest connection is not original array
decrease connection by one until folder found in array
if we decrease until the until connection is nothing
the path is what we started with + child node
*/
folders_ = folder;
graph = std::unordered_map<std::string, std::vector<std::string>>{};
in_out = std::unordered_map<std::string, std::pair<int, int>>{};
auto root_folders = std::vector<std::string>{};
for (auto& f : folder) {
auto curr = std::string{};
auto prev = std::string{};
for (auto i = 0; i < f.size(); ++i) {
if (f[i] == '/') {
if (!curr.empty()) {
if (!graph.contains(curr)) {
root_folders.push_back(curr);
graph[curr] = {};
}
if (!in_out.contains(curr))
in_out[curr] = {0, 0};
if (!prev.empty()) {
if (!graph.contains(prev))
graph[prev] = {};
if (!in_out.contains(prev))
in_out[prev] = {0, 0};
graph[prev].push_back(curr);
in_out[prev].second++;
in_out[curr].first++;
}
prev = curr;
}
curr.clear();
} else {
curr += f[i];
}
}
if (!curr.empty()) {
if (!graph.contains(curr)) {
root_folders.push_back(curr);
graph[curr] = {};
}
if (!in_out.contains(curr))
in_out[curr] = {0, 0};
if (!prev.empty()) {
if (!graph.contains(prev))
graph[prev] = {};
if (!in_out.contains(prev))
in_out[prev] = {0, 0};
graph[prev].push_back(curr);
in_out[prev].second++;
in_out[curr].first++;
}
}
}
super_folders = std::vector<std::string>{};
std::sort(root_folders.begin(), root_folders.end(), [this](std::string a, std::string b) {
return this->in_out[a].second < this->in_out[b].second;
});
for (auto& f : root_folders) {
auto seen = std::unordered_set<std::string>{};
dfs(f, seen, "/" + f);
}
return {};
}
private:
void dfs(std::string node, std::unordered_set<std::string>& seen, std::string path) {
if (seen.contains(node))
return;
seen.insert(node);
auto common_path = false;
if (graph[node].size() > 1) {
// split in path found
while (std::find(folders_.begin(), folders_.end(), path) == folders_.end()) {
if (path.empty())
break;
path = path.substr(0, path.size() - 2);
}
if (path.empty())
common_path = true;
else {
super_folders.push_back(path);
return;
}
}
for (auto& n : graph[node]) {
if (common_path)
super_folders.push_back(path + "/" + n);
else
dfs(n, seen, path + "/" + n);
}
}
std::vector<std::string> folders_;
std::unordered_map<std::string, std::vector<std::string>> graph;
std::unordered_map<std::string, std::pair<int, int>> in_out;
std::vector<std::string> super_folders;
};
// Solution 2
class Solution {
public:
std::vector<std::string> removeSubfolders(std::vector<std::string>& folder) {
// Sort folders lexicographically
std::sort(folder.begin(), folder.end());
// Vector to store the result
std::vector<std::string> result;
// Add the first folder to the result as it's always a root folder
result.push_back(folder[0]);
// Iterate through the folders and check if each is a subfolder of the previous one
for (int i = 1; i < folder.size(); ++i) {
// Check if the current folder starts with the last folder in result followed by a '/'
if (folder[i].substr(0, result.back().size()) != result.back() || folder[i][result.back().size()] != '/') {
result.push_back(folder[i]);
}
}
return result;
}
};