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?
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
temp.f95:6:30: 6 | integer :: row(rowIndex+1) | 1 Error: Explicit shaped array with nonconstant bounds at (1) temp.f95:10:32: 10 | call solve(rowIndex, row) | 1 Error: Symbol ‘row’ at (1) has no IMPLICIT type temp.f95:45:36: 44 | do i = 2, rowIndex+1 | 2 45 | row(i) = row(i-1) + row(i-2) | 1 Warning: Array reference at (1) out of bounds (0 < 1) in loop beginning at (2)
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
temp.f95:20:12: 20 | do i = 2, rowIndex | 1 Error: Symbol ‘i’ at (1) has no IMPLICIT type temp.f95:28:9: 28 | use PascalsTriangle | 1 Fatal Error: Cannot open module file ‘pascalstriangle.mod’ for reading at (1): No such file or directory compilation terminated.
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.
#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.