-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy paths1.cpp
More file actions
25 lines (25 loc) · 765 Bytes
/
Copy paths1.cpp
File metadata and controls
25 lines (25 loc) · 765 Bytes
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
// OJ: https://leetcode.com/problems/longest-absolute-file-path/
// Author: github.com/lzl124631x
// Time: O(N)
// Space: O(N)
class Solution {
public:
int lengthLongestPath(string input) {
stack<int> len;
stringstream ss(input);
string line;
int ans = 0;
while (getline(ss, line)) {
int lv = 0;
while (lv < line.size() && line[lv] == '\t') ++lv;
while (len.size() > lv) len.pop();
int pathLen = len.empty() ? 0 : len.top(), curLen = line.size() - lv;
if (line.find('.') != string::npos) {
ans = max(ans, pathLen + curLen);
} else {
len.push(pathLen + 1 + curLen);
}
}
return ans;
}
};