How to optimize this solution?


  • 0
    R

    How can I optimize this solution ??

    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> result;
        if (numRows < 1)
            return result;
        
        //vector<int> currRow;
        //currRow.push_back(1);
        result.push_back({1});
        
        for (int i = 1; i < numRows; i++)
        {
            //vector<int> currRow;
            result.push_back({});
            //currRow.push_back(1);
            result[i].push_back(1);
            for (int j = 1; j < result[i-1].size(); j++)
            {
                //currRow.push_back(result[i-1][j-1] + result[i-1][j]);
                result[i].push_back(result[i-1][j-1] + result[i-1][j]);
            }
            //currRow.push_back(1);            
            result[i].push_back(1);   
            //result.push_back(currRow);
        }
        return result;
    }

Log in to reply
 

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