So, the number of columns is different for each row. In this array, the number of columns (say, numCols) is equal to the number of the i-th row + 1 (Since, 0-indexed), i.e., for 0-th row, numCols = 1. This array is used to store each of the rows expected in the output, so, for example, array =. Now, what about the inner elements? Well, any inner element is obtained by doing the sum of the 2 values present in the row just above it, i.e., if the element is at index (i, j), then matrix can be obtained by doing matrix + matrix.Īpproach: To solve the problem, we need to first create an array of size N or numRows (input value). So, first things first, if you are at the edge of the matrix, the value is 1, that’s for sure. Think about the image as a matrix now where each line is basically a row in the matrix. Intuition: When you see the image above, you get a pretty good idea of what you are supposed to do here. Each row corresponds to each one of the rows in the image shown above.ĭisclaimer: Don’t jump directly to the solution, try it out yourself first. In Pascal’s triangle, each number is the sum of the two numbers directly above it as shown in the figure below:Įxplanation: There are 5 rows in the output matrix. Problem Statement: Given an integer N, return the first N rows of Pascal’s triangle.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |