leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0388.cpp (978B)


0 class Solution {
1 public:
2 int lengthLongestPath(const string &input) const {
3 const int n = size(input);
4 stack<pair<int, int>> st;
5 int res = 0;
7 st.emplace(0, 0);
8 for (int i = 0, dir_len = 0; i < n;) {
9 if (input[i] == '\n') {
10 int cnt = 0;
11 while (++i < n && input[i] == '\t')
12 cnt++;
14 while (cnt < st.top().first)
15 st.pop();
16 if (cnt > st.top().first) st.emplace(cnt, st.top().second + dir_len + 1);
17 } else {
18 int cnt = 0, is_file = 0;
20 while (i < n && input[i] != '\n') {
21 if (input[i] == '.') is_file = true;
22 i++, cnt++;
23 }
25 if (is_file)
26 res = max(res, st.top().second + cnt);
27 else
28 dir_len = cnt;
29 }
30 }
32 return res;
33 }
34 };