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)


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