Skip to content

Commit 806b8b3

Browse files
committed
Add Pascal triangle algorithm
1 parent 54dce7a commit 806b8b3

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

src/images/pascal-triangle.png

15 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package java1.algorithms.array.pascalsTriangle;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class PascalsTriangle {
7+
private static List<List<Integer>> generatePascalTriangle(int numRows){
8+
List<List<Integer>> triangle = new ArrayList<>();
9+
10+
for (int row = 0; row < numRows; row++) {
11+
List<Integer> currentRow = new ArrayList<>(row+1);
12+
currentRow.add(1);
13+
for (int col = 1; col < row; col++) {
14+
currentRow.add(triangle.get(row-1).get(col-1) + triangle.get(row-1).get(col));
15+
}
16+
if(row >0) {
17+
currentRow.add(1);
18+
}
19+
triangle.add(currentRow);
20+
}
21+
return triangle;
22+
}
23+
public static void main(String[] args) {
24+
int numRows1 = 5;
25+
System.out.println(generatePascalTriangle(numRows1));
26+
int numRows2 = 1;
27+
System.out.println(generatePascalTriangle(numRows2));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
**Description:**
2+
Given an integer `numRows`, find the first `numRows` of Pascal's triangle. In Pascal's triangle, each number is formed by the sum of the two numbers directly above it except for boundaries(with value 1).
3+
4+
![Screenshot](../../../../images/pascal-triangle.png)
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: numRows = 5
10+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
11+
12+
Example 2:
13+
14+
Input: numRows = 1
15+
Output: [[1]]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of sequential numbers sum and average calculations. The algorithmic approach can be summarized as follows:
19+
20+
1. Initialize the triangle(`triangle`) with an empty to hold the rows of Pascal triangle.
21+
22+
2. Iterate over the rows starting from `0` to the given number of rows(`numRows`) provided.
23+
24+
3. Create each row initialized with `row+1` array elements and the first element is set to 1. This is because all border elements will be 1 in the triangle.
25+
26+
4. Iterate over the columns starting from `1` to `row-1` to calculate the new row values. Each cell value is calculated by the sum of two elements directly above it from the previous row.
27+
28+
5. Set the last element of each row to 1, except for the first row.
29+
30+
6. Add the completed row to the triangle list.
31+
32+
7. Return the completed triangle upon generation of each row completed.
33+
34+
**Time and Space complexity:**
35+
This algorithm has a time complexity of `O(n^2)`, where `n` is the number of given rows. This is because we need to iterate over `n` rows and for each row, there can be upto `n` valued to be generated.
36+
37+
The output array needs `O(n^2)` space to accomodate all the triangle elements, where `n` is the number of given rows.. Hence, the total space complexity is `O(n^2)`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function generatePascalsTriangle(numRows){
2+
let triangle = [];
3+
4+
for (let row = 0; row < numRows; row++) {
5+
let currentRow = Array(row+1).fill(1);
6+
for (let col = 1; col < row; col++) {
7+
currentRow[col] = triangle[row-1][col-1]+triangle[row-1][col];
8+
}
9+
triangle.push(currentRow);
10+
}
11+
12+
return triangle;
13+
}
14+
15+
let numRows1 = 5;
16+
console.log(generatePascalsTriangle(numRows1));
17+
let numRows2 = 1;
18+
console.log(generatePascalsTriangle(numRows2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
**Description:**
2+
Given an integer `numRows`, find the first `numRows` of Pascal's triangle. In Pascal's triangle, each number is formed by the sum of the two numbers directly above it except for boundaries(with value 1).
3+
4+
![Screenshot](../../../../images/pascal-triangle.png)
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: numRows = 5
10+
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
11+
12+
Example 2:
13+
14+
Input: numRows = 1
15+
Output: [[1]]
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of sequential numbers sum and average calculations. The algorithmic approach can be summarized as follows:
19+
20+
1. Initialize the triangle(`triangle`) with an empty to hold the rows of Pascal triangle.
21+
22+
2. Iterate over the rows starting from `0` to the given number of rows(`numRows`) provided.
23+
24+
3. Create each row initialized with `row+1` array elements, all set to 1.
25+
26+
4. Iterate over the columns starting from `1` to `row-1` to calculate the new row values. Each cell value is calculated by the sum of two elements directly above it from the previous row.
27+
28+
5. Add the completed row to the triangle list.
29+
30+
6. Return the completed triangle upon generation of each row completed.
31+
32+
**Time and Space complexity:**
33+
This algorithm has a time complexity of `O(n^2)`, where `n` is the number of given rows. This is because we need to iterate over `n` rows and for each row, there can be upto `n` valued to be generated.
34+
35+
The output array needs `O(n^2)` space to accomodate all the triangle elements, where `n` is the number of given rows.. Hence, the total space complexity is `O(n^2)`.

0 commit comments

Comments
 (0)