Tensorum
YouTubeशिकायला सुरुवात करा
LeetCode 119 · Triangle / Pascal

Pascal's Triangle II

LeetCode 119 · सोपा · उपाय, intuition आणि टप्प्याटप्प्याने वॉकथ्रू

C(r, i+1) = C(r, i) · (r − i) / (i + 1).

सोपाArrayDynamic Programming

मी Pascal's Triangle II (LeetCode 119) असा सोडवला — Triangle / Pascal पॅटर्नमधील एक सोपा प्रॉब्लेम. खाली: intuition, टप्प्याटप्प्याने approach, माझा accepted Python कोड, animated test cases आणि line-by-line debugger.

खालील विवरण इंग्रजीत आहे.

Intuition

Row r of Pascal's triangle is C(r, 0), C(r, 1), …, C(r, r). Each coefficient comes from the previous one by multiplying by (r − i) and dividing by (i + 1), so the row can be produced in a single pass without building the triangle above it.

Approach

  1. 1

    Start the row with 1.

  2. 2

    For i from 0 to r-1, update the running coefficient with multiply-then-divide.

  3. 3

    Append each coefficient to the row.

मी कसा सोडवला

Running nCrPythonO(r) time · O(1) extra space
class Solution:
    def getRow(self, rowIndex: int) -> List[int]:
        res=[1]
        ans=1
        for i in range(rowIndex):
            ans=ans*(rowIndex-i)
            ans= ans//(i+1)
            res.append(ans)
        return res
स्टेप-थ्रू लोड होत आहे…

Pascal's Triangle II — वारंवार विचारले जाणारे प्रश्न

Pascal's Triangle II मागील intuition काय आहे?

Row r of Pascal's triangle is C(r, 0), C(r, 1), …, C(r, r). Each coefficient comes from the previous one by multiplying by (r − i) and dividing by (i + 1), so the row can be produced in a single pass without building the triangle above it.

Pascal's Triangle II (LeetCode 119) कसा सोडवावा?

1. Start the row with 1. 2. For i from 0 to r-1, update the running coefficient with multiply-then-divide. 3. Append each coefficient to the row.

Pascal's Triangle II ची time आणि space complexity किती आहे?

Time: O(r). Space: O(r) for the output. (Accepted approach: Running nCr.)

Pascal's Triangle II मध्ये सामान्य चुका कोणत्या?

Multiply before dividing so the integer division stays exact.