↓Skip to main content
  1. LeetCode/

LeetCode 119: Pascal's Triangle II

·2 mins· ·
LeetCode Easy Array Dynamic-Programming
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2022-08-16

  • Submissions: Wrong Answer ×2 → Accepted ×2 → Runtime Error → Wrong Answer → Runtime Error → Accepted ×7

Approach
#

For a single row there is no need to store the whole triangle: keep one array, append a 1 per row, then update row[j] += row[j - 1] from right to left (Linear DP Template 1), so row[j - 1] still holds the previous row’s value; left to right would reuse already-updated values. rowIndex is 0-based, so row k has k + 1 numbers.

My Attempts
#

  • Attempt 1 built the triangle as in LC 118 but only rowIndex rows; rowIndex is 0-based, so it returned the previous row: rowIndex = 5 gave [1, 4, 6, 4, 1].
  • Then I tried the formula ans[i] = ans[i - 1] * up / down: a wrong parameter name (NameError), floats from / judged wrong, and map(ans, int) with the arguments reversed (TypeError); // passed.

Solution
#

class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        row = [1]
        for _ in range(rowIndex):
            row.append(1)
            for j in range(len(row) - 2, 0, -1):
                row[j] += row[j - 1]
        return row

Complexity
#

Time O(k²), space O(k).

Mistakes to Avoid
#

  • rowIndex is 0-based; build rowIndex + 1 rows.
  • The 1D update runs right to left.
  • With the formula C(k, i), use integer division; / gives floats.

Interview Explanation
#

I keep one array, append a 1 per row, and add each cell’s left neighbor from right to left so only previous-row values are used, in O(k) space; the binomial coefficients C(k, i) also work term by term.

Related

LeetCode 118: Pascal's Triangle
·1 min
LeetCode Easy Array Dynamic-Programming
LeetCode 118 note: each row starts and ends with 1, and every inner cell is the sum of the two cells above it
LeetCode 108: Convert Sorted Array to Binary Search Tree
·1 min
LeetCode Easy Array Divide-and-Conquer Tree Binary-Search-Tree
LeetCode 108 note: the middle element becomes the root and each half recurses; the halves differ by at most one, so the tree is balanced
LeetCode 27: Remove Element
·1 min
LeetCode Easy Array Two-Pointers
LeetCode 27 note: the write pointer copies only values not equal to val, returning how many were written
LeetCode 35: Search Insert Position
·2 mins
LeetCode Easy Array Binary-Search
LeetCode 35 note: find the first position >= target (lower bound); a half-open [l, r) needs r = mid, not r = mid - 1
LeetCode 66: Plus One
·2 mins
LeetCode Easy Array Math
LeetCode 66 note: add one from the last digit, turning 9s into 0s while carrying, and prepend 1 when every digit was 9
LeetCode 26: Remove Duplicates from Sorted Array
·2 mins
LeetCode Easy Array Two-Pointers
LeetCode 26 note: overwrite in place with a write pointer for the next slot, writing only values different from the last one written