Pascal's Triangle II

🏠 ⬅️ ➡️

Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.

In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:

Example 1:

Input: rowIndex = 3 Output: [1,3,3,1]

Example 2:

Input: rowIndex = 0 Output: [1]

Example 3:

Input: rowIndex = 1 Output: [1,1]

Constraints:

  • 0 <= rowIndex <= 33

Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?


Note: This problem is from LeetCode.
Compiled
Executed
Correct
program pascals_triangle
    implicit none

    integer :: rowIndex
    integer :: i, j
    integer :: row(rowIndex+1)

    do while (get_next_example())
        read (*, *) rowIndex
        call solve(rowIndex, row)
        write (*, '(*(I0, 1X))') row
    end do

contains

    logical function get_next_example()
        implicit none
        character(len=100) :: line
        integer :: status

        read (*, '(A)', iostat=status) line
        if (status /= 0) then
            get_next_example = .false.
            return
        end if

        if (line == '') then
            get_next_example = .false.
            return
        else
            get_next_example = .true.
            return
        end if
    end function get_next_example

    subroutine solve(rowIndex, row)
        implicit none
        integer, intent(in) :: rowIndex
        integer, intent(out) :: row(:)

        integer :: i

        row(1) = 1
        do i = 2, rowIndex+1
            row(i) = row(i-1) + row(i-2)
        end do
    end subroutine solve
end program pascals_triangle
Compiled
Executed
Correct
module PascalsTriangle
    implicit none
    private
    public :: getRow
contains
    function getRow(rowIndex) result(row)
        integer, intent(in) :: rowIndex
        integer :: row(rowIndex+1)

        ! Edge case: If rowIndex is 0, the triangle has only one row
        if (rowIndex == 0) then
            row = [1]
            return
        end if

        ! Initialize the first two rows of the triangle
        row = [1, 1]

        ! Loop through the rows of the triangle
        do i = 2, rowIndex
            ! Calculate the current row of the triangle
            row(i) = (row(i-1) * (i-1)) / (i)
        end do
    end function getRow
end module PascalsTriangle

program test
    use PascalsTriangle
    implicit none

    ! Test case 1: rowIndex = 3
    write (*,*) getRow(3)

    ! Test case 2: rowIndex = 0
    write (*,*) getRow(0)

    ! Test case 3: rowIndex = 1
    write (*,*) getRow(1)
end program test
🌐 Data from online sources
def get_row(rowIndex):
    row = [1] * (rowIndex + 1)
    for i in range(1, rowIndex + 1):
        for j in range(i - 1, 0, -1):
            row[j] += row[j - 1]
    return row

The algorithm initializes the row with the given rowIndex size and fills it with 1s. Then, we iterate through the rowIndex and update each entry of the row using the sum of the two numbers directly above it (based on Pascal's triangle property), i.e., row[j] = row[j] + row[j-1]. We get the rowIndexth row of Pascal's triangle once the iterations are completed.

🌐 Data from online sources
#include <vector>
using namespace std;

vector<int> getRow(int rowIndex) {
    vector<int> row(rowIndex + 1, 1);
    for (int i = 1; i <= rowIndex; i++) {
        for (int j = i - 1; j > 0; j--) {
            row[j] += row[j - 1];
        }
    }
    return row;
}

The algorithm initializes the row with the given rowIndex size and fills it with 1s. Then, we iterate through the rowIndex and update each entry of the row using the sum of the two numbers directly above it (based on Pascal's triangle property), i.e., row[j] = row[j] + row[j-1]. We get the rowIndexth row of Pascal's triangle once the iterations are completed.