← Home

119. Pascal's Triangle II

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
straightforward implementationC++Markdown
119

This problem looks busy at first, but the accepted solution is built around one steady invariant. For 119. Pascal's Triangle II, the solution in this repository is mainly a straightforward implementation solution.

Guide

What?

We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: straightforward implementation.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are getRow, row.

Guide

Why?

The win comes from making each line carry responsibility: store the useful state, discard the rest, keep moving.

  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. Let the final stored value answer the original question.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Pascal's Triangle II.
02//Memory Usage: 8.6 MB, less than 30.81% of C++ online submissions for Pascal's Triangle II.
03
04class Solution {
05public:
06    vector<int> getRow(int rowIndex) {
07        vector<vector<int>> pascal = {{1},
08                                      {1,1}};
09        
10        for(int i = 2; i <= rowIndex; i++){
11            //the length of ith row is i+1
12            vector<int> row(i+1, 1);
13            
14            for(int j = 1; j < i+1-1; j++){
15                row[j] = pascal[i-1][j-1] + pascal[i-1][j];
16            }
17            
18            pascal.push_back(row);
19        }
20        
21        return pascal[rowIndex];
22    }
23};
24
25//solution from discusion
26
27//Runtime: 4 ms, faster than 100.00% of C++ online submissions for Pascal's Triangle II.
28//Memory Usage: 8.5 MB, less than 62.09% of C++ online submissions for Pascal's Triangle II.
29
30/**
31class Solution {
32public:
33    vector<int> getRow(int rowIndex) {
34        vector<int> ans(rowIndex+1);
35        ans[0] = 1;
36        
37        for(int i = 1; i < rowIndex + 1; i++){
38            for(int j = i; j >= 1; j--){
39                ans[j] += ans[j-1];
40            }
41        }
42        return ans;
43    }
44};
45**/

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.