-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest-absolute-file-path.cpp
68 lines (62 loc) · 1.4 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <string>
#include <vector>
using namespace std;
// https://leetcode.com/problems/longest-absolute-file-path/
class Solution {
public:
int lengthLongestPath(string input) {
int lngst = 0;
bool file = false;
vector<int> plen({0});
vector<string> ppath({""});
int len = 0;
vector<int> depth({0});
string path;
for (size_t i = 0; i < input.size(); ++i) {
char ch = input[i];
switch (ch) {
case '.':
file = true;
len++;
// path += ch;
break;
case '\n': {
int new_depth = 0;
for (size_t j = i + 1; j < input.size() && '\t' == input[j];
j++, new_depth++)
;
for (; depth.back() > new_depth;) {
depth.pop_back();
plen.pop_back();
// ppath.pop_back();
}
if (new_depth > depth.back()) {
depth.push_back(new_depth);
plen.push_back(1 + len);
// ppath.push_back( ppath.back() + "/" + path );
len = plen.back();
} else {
len = plen.back();
}
i += new_depth;
// path = "";
file = false;
break;
}
default:
len++;
// path += ch;
break;
}
if (file) {
lngst = max(len, lngst);
}
}
return lngst;
}
};