Skip to content

Latest commit

 

History

History
40 lines (29 loc) · 906 Bytes

12.md

File metadata and controls

40 lines (29 loc) · 906 Bytes

Method 1 (TC-O(n^2))

vector<int> getRow(int rowIndex) {
           
        vector<vector<int>> ans(rowIndex + 1);

        ans[0].push_back(1);
        
        for (int i = 1; i <= rowIndex; i++) {
            ans[i].push_back(1);
            for (int j = 1; j < i; j++) {
                ans[i].push_back(ans[i - 1][j - 1] + ans[i - 1][j]);
            }
            ans[i].push_back(1);
        }

        return ans[rowIndex];
    
    }

Method 2 (TC-O(n))

std::vector<int> getRow(int rowIndex) {
        std::vector<int> res(1, 1);
        long long prev = 1;
        for(int k = 1; k <= rowIndex; k++) {
            long long next_val = prev * (rowIndex - k + 1) / k;
            res.push_back(next_val);
            prev = next_val;
        }
        return res;
    }