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.