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.