Pascal's Triangle using Golang

 Solving Pascal's Triangle with golang, I solved this problem using two way, first way is to normal handle through iteration and second by using Dynamic programming.
Problem Statement:

Pascal's Triangle

Solution 1: without DP


func helper(index int, temp []int)[]int{
arr := make([]int,0)
arr = append(arr,1)
for i:=1;i<index;i++{
arr = append(arr,temp[i]+temp[i-1])
}
arr = append(arr,1)
return arr
}


func generate(numRows int) [][]int {
result := make([][]int,0)
pascalRow := make([]int,0)
pascalRow = append(pascalRow, 1)
result = append(result,pascalRow)
for i:=1;i<numRows;i++{
pascalRow = helper(i,pascalRow)
result = append(result,pascalRow)
}
return result
}

Post a Comment

0 Comments