Directory Sum

Problem 1: The idea I used was to maintain the max while removing duplicates, and converting back to the original string..

int solution(int X) {
    int max = INT_MIN;
    string str = to_string(X);
    string str_cpy = str;
    bool removed = false;
    for (int i = 1; i < str.length(); i++) {
        if (str.at(i - 1) == str.at(i)) {
            removed = true;
            str.erase(str.begin() + (i - 1), str.begin() + i);
            if (atoi(str.c_str()) > max) {
                max = atoi(str.c_str());
            }
            str = str_cpy;
            while (i < str.length() && str.at(i - 1) == str.at(i)) {
                i++;
            }
        }
    }
    if (!removed) return X;
    return max;
}


int main() {
    cout << solution(223336226) << endl;
    pause();
    cout << solution(123456789) << endl;
    pause();
}

Problem 2: http://pastebin.com/TCty5ggg Bad implementation -> not O(n) time!

  • The idea: Consider the following example:

We can arrange the following tables through a preprocessing step

  • The basic idea is for each img file, we match the closest line that is less that is at least 1 less then the img line that also contains 1 less space than it.

  • We continue looking for 1 less space until all the spaces are exhausted (root directory is reached).

Problem 2: Attempt 2

  • More simplified. Uses a stack to mimic tree structure.

  • To understand this we first should recognize a few things:

    • One, a file system is effectively a tree. A graph too, but more simply, a tree. Our algorithm simplifies a lot this way.

For example, this file structure is effectively:

  • Next, what distinguishes a node and its children are the number of tabs in the tree of each line.

  • Notice that the child of every parent is 1 tab more than it. Using this as our guiding principle, we can model our tree.

  • So the solution is just iterating through the entire tree, and keeping track of the largest path for a file.

  • A file is distinguished with a '.'

  • Because our 'tree' is a txt file, we'll be modeling our tree using a stack, which is just effective as using recursion.

    • Our stack begins with the root directory, and we'll be adding elements to our stack in a pre-order fashion. That is, elements continue to be added, until we find that the next child is not a child of the previous top element. We do this by comparing the number of tabs of the current node, to the top of the stack.

    • In the process, we collect the size on the current file path. And if the node is a file, we update the current max path.

    • Otherwise, if our node is not a child, we'll have to 'recurse back', until we find a position where it is. That is, our stack can only grow in the number of tabs it has. For example, we can have 1->2->3->4... but not 1->2->2. The second while loop in the code takes care of this for us.

      • For example, consider that we are in subsubdir1 in the tree above. In other to get to subdir2, it would not make sense just to recurse back once and go to subdir1. We'll have to continue recursing back until subdir2 tabs are > dir1.

Problem 2: Attempt 3

Similar idea as before, just cleaned up. The algorithm comes down to knowing which position in the current file is by using the number of tabs as a guiding principle.

Complexity: O(n) time and space

Last updated

Was this helpful?