C++ one vector solution


  • 0
    A

    Time complexity O(n), space complexity O(depth of path).

    25 / 25 test cases passed
    Status: Accepted
    Runtime: 0 ms

    class Solution {
    public:
        int lengthLongestPath(string input) {
            input.push_back('\n');
            vector<int> v(1, -1);
            uint pos = 1, len = 0;
            for (uint i = 0, ii = input.size(), b = 0, f = 0; i < ii; i++) {
                if (input[i] == '\n') {
                    uint tmp = v[pos - 1] + i - b + 1;
                    if (v.size() == pos) v.push_back(tmp);
                    else v[pos] = tmp;
                    if (f && v[pos] > len) len = v[pos];
                    f = 0;
                    pos = 1;
                    b = i + 1;
                } else if (input[i] == '\t') {
                    pos++;
                    b = i + 1;
                } else if (input[i] == '.') f = 1;
            }
            return len;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.