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
LeetCode-এ খুলুন ↗

আমি 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.