# 119 Pascal's Triangle II

Given an index k, return the kth row of the Pascal's triangle.

For example, given k = 3, Return \[1,3,3,1].

Note: Could you optimize your algorithm to use only O(k) extra space?

**The Idea:** Pascal's Triangle can be built off the previous case, so we can iterate to the kth row dynammically. Add 0's to the edges to make things easier and for clean. We can cut these off at the end.

**Complexity:** O(row\_index \* (sum i = 1 to row\_index (length(root))) time and constant space

```cpp
vector<int> getRow(int rowIndex) {
    vector<int> root = { 0, 1, 0 };
    for (int row = 1; row <= rowIndex; row++) {
        vector<int> new_root;
        new_root.reserve(root.size() + 2);
        new_root.push_back(0);

        for (int i = 0; i < root.size() - 1; i++) {
            new_root.push_back(root[i] + root[i + 1]);
        }

        new_root.push_back(0);
        root = new_root;
    }

    return vector<int>(root.begin() + 1, root.end() - 1);
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://maksimdan.gitbook.io/interview-practice-problems/leetcode_sessions/119-pascals-triangle-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
