-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathLongest Absolute File Path.cpp
53 lines (50 loc) · 1.28 KB
/
Longest Absolute File Path.cpp
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
// Runtime: 0 ms (Top 100.00%) | Memory: 6.5 MB (Top 50.23%)
// Using Map O(300 + N)
class Solution {
public:
int lengthLongestPath(string input) {
input.push_back('\n');
vector<int> levels(301, 0);
int ans = 0;
int curr_tabs = 0;
bool is_file = false;
int curr_word_len = 0;
int total_len = 0;
for(char c : input)
{
if(c == '\t')
{
curr_tabs++;
}
else if(c == '\n')
{
if(curr_tabs == 0)
{
levels[0] = curr_word_len;
}
else
{
levels[curr_tabs] = levels[curr_tabs-1] + curr_word_len;
}
if(is_file)
{
ans = max(ans, levels[curr_tabs] + curr_tabs);
// levels[curr_tabs] = 0;
}
curr_tabs = 0;
is_file = false;
curr_word_len = 0;
}
else if(c == '.')
{
is_file = true;
curr_word_len++;
}
else
{
curr_word_len++;
}
}
return ans;
}
};