-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
388.cpp
99 lines (95 loc) · 2.9 KB
/
388.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
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
__________________________________________________________________________________________________
sample 4 ms submission
class Solution {
public:
int lengthLongestPath(string input) {
int n = input.size();
unordered_map<int,int> lengths;
lengths[-1] = 0;
int length = 0, level = 0, max_length = 0;
bool is_file = false;
for (int i = 0; i < n; ++i) {
switch (input[i]) {
case '\n':
if (is_file) {
max_length = max(max_length, length + lengths[level - 1]);
}
else {
lengths[level] = length + lengths[level - 1] + 1; // include slash
}
level = 0;
length = 0;
is_file = false;
break;
case '\t':
++level;
break;
case '.':
is_file = true;
++length;
break;
default:
++length;
}
}
// Recheck since no \n at end of input
return (is_file ? max(max_length, length + lengths[level - 1]) : max_length);
}
};
__________________________________________________________________________________________________
sample 8708 kb submission
class Solution {
public:
int lengthLongestPath(string input) {
vector<int> levelLen;
int currentLen = 0;
int currentLevel = 0;
int maxLen = 0;
bool isfile = false;
for (char a: input)
{
if (a == '\n')
{
if (levelLen.size() <= currentLevel)
levelLen.push_back(currentLen);
else
levelLen[currentLevel] = currentLen;
if(isfile)
{
int sum = 0;
for (int i=0; i<=currentLevel; i++)
sum += levelLen[i]+1;
maxLen = max(maxLen, sum-1);
isfile = false;
}
currentLen = 0;
currentLevel = 0;
}
else if(a == '\t')
{
currentLevel++;
}
else
{
currentLen++;
if(a == '.')
isfile = true;
}
}
//handle the last file
if(isfile)
{
if (levelLen.size() < currentLevel + 1)
levelLen.push_back(currentLen);
else
levelLen[currentLevel] = currentLen;
int sum = 0;
for (int i=0; i<=currentLevel; i++)
sum += levelLen[i]+1;
maxLen = max(maxLen, sum-1);
isfile = false;
}
return maxLen;
}
};
__________________________________________________________________________________________________