Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
1.3 kB
2
Indexable
Never
class Solution {
public:
    int start_index_func(int num_rows){
        int start_index = (num_rows-1)*num_rows/2;
        return start_index;
    }
    int end_index_func(int num_rows){
        int end_index = (num_rows)*(num_rows+1)/2-1;
        return end_index;
    }
    int total_value_func(int num_rows){
        int total_value = num_rows*(num_rows+1)/2;
        return total_value;
    }
    vector<vector<int>> generate(int numRows) {
        vector<vector<int>> ans = {{1}};
        if(numRows == 1){
            return ans;
        }
        int total_value = total_value_func(numRows);
        int ans_array[total_value];
        ans_array[0] = 1;

        for(int row_no=2; row_no<=numRows ; row_no++){
            int start_index = start_index_func(row_no);
            int end_index = end_index_func(row_no);
            ans_array[start_index] = 1;
            ans_array[end_index] = 1;
            vector<int> temp_vec;
            temp_vec.push_back(1);
            for(int rem_index = start_index+1; rem_index<end_index ; rem_index++){
                ans_array[rem_index] = ans_array[rem_index-row_no] + ans_array[rem_index-row_no+1];
                temp_vec.push_back(ans_array[rem_index]);
            }
            temp_vec.push_back(1);
            ans.push_back(temp_vec);
        }
        return ans;
    }
};