Simple 3ms C++ solution using HashMap


  • 0
    V
    map<int, vector<int>> m;
        void goDown(vector<NestedInteger>& L, int depth) {
            if (!L.size())
                return;
            for (auto x : L) {
                if (x.isInteger())
                    m[depth].push_back(x.getInteger());
                else
                    goDown(x.getList(), depth + 1);
            }
        }
        
        int depthSumInverse(vector<NestedInteger>& nl) {
            int ans = 0, map_max = INT_MIN;
            goDown(nl, 1);
            for (auto x : m)
                map_max = max(map_max, x.first);
            for (auto x : m) {
                int val = 0;
                for (auto p : x.second)
                    val += p;
                ans += (val * (map_max - x.first + 1));
            }
            return ans;
        }
    

Log in to reply
 

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