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.