Why fails in leetcode while succeeds in eclipse?


  • 0
    R

    In Eclipse the following code gives me 44 which is the correct answer for the input [115,215,224,325,336,446,458]

    In leetcode it gives 21 though! Anyone knows why?

    void pathSum(vector<vector<int> >& levels, int level, int pos, int sum, int& allSum) {
        if(level == levels.size() || levels[level][pos] == -1) return;
        sum+= levels[level][pos];
        if(level+1 < levels.size() && levels[level+1][pos*2] == -1 && levels[level+1][pos*2+1] == -1 )
            allSum+=sum;
    
        pathSum(levels, level+1, pos*2, sum, allSum);
        pathSum(levels, level+1, pos*2+1, sum, allSum);
    }
    
    int pathSum(vector<int> nums) {
        vector<vector<int> > levels(6, vector<int>(8, -1));
        int d, pos, v;
    
        for(int i = 0; i < nums.size(); i++) {
            d = nums[i] / 100 - 1;
            pos = ((nums[i] / 10) % 10) - 1;
            v = nums[i] % 10;
            levels[d][pos] = v;
        }
    
        int allsum = 0;
        pathSum(levels, 0, 0, 0, allsum);
        return allsum;
    }

  • 0
    R

    I guess I figured it out.

    vector<vector<int> > levels(6, vector<int>(8, -1));
    should be
    vector<vector<int> > levels(5, vector<int>(16, -1));


Log in to reply
 

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