Pascal's Triangle II
LeetCode 119 · సులభం · పరిష్కారం, intuition & దశలవారీ వాక్త్రూ
C(r, i+1) = C(r, i) · (r − i) / (i + 1).
నేను 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
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.
నేను ఎలా పరిష్కరించాను
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 resPascal'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.