Question

In: Computer Science

Pascal’s triangle looks as follows: 1 1 1 1 2 1 1 3 3 1 1...

Pascal’s triangle looks as follows:

1

1 1

1 2 1

1 3 3 1

1 4 6 4 1

...

The first entry in a row is 1 and the last entry is 1 (except for the first row which contains only 1), and every other entry in Pascal’s triangle is equal to the sum of the following two entries: the entry that is in the previous row and the same column, and the entry that is in the previous row and previous column.

Use dynamic programming to design an O(n2) time algorithm that computes the first n rows in Pascal’s triangle.

Solutions

Expert Solution

import java.util.ArrayList;

import java.util.List;

public class Main {

public static List<List<Integer>> pascalTriangle(int n) {

List<List<Integer>> triangle = new ArrayList<List<Integer>>();

if (n == 0) {

return triangle;

}

triangle.add(new ArrayList<>());

triangle.get(0).add(1);

for (int rowNum = 1; rowNum < n; rowNum++) {

List<Integer> row = new ArrayList<>();

List<Integer> prevRow = triangle.get(rowNum-1);

row.add(1);

for (int j = 1; j < rowNum; j++) {

row.add(prevRow.get(j-1) + prevRow.get(j));

}

row.add(1);

triangle.add(row);

}

return triangle;

}

public static void main(String[] args) {

int n = 10;

List<List<Integer>> triangle = pascalTriangle(n);

for (List<Integer> row : triangle) {

for (int el : row) {

System.out.print(el + " ");

}

System.out.println();

}

}

}


Related Solutions

Use Pascal’s triangle to find the possible value(s) of nn if nC3=(n3)=20.
Use Pascal’s triangle to find the possible value(s) of nn if nC3=(n3)=20.
Prove via induction the following properties of Pascal’s Triangle: •P(n,2)=(n(n-1))/2 • P(n+m+1,n) = P(n+m,n)+P(n+m−1,n−1)+P(n+m−2,n−2)+···+P(m,0) for all...
Prove via induction the following properties of Pascal’s Triangle: •P(n,2)=(n(n-1))/2 • P(n+m+1,n) = P(n+m,n)+P(n+m−1,n−1)+P(n+m−2,n−2)+···+P(m,0) for all m ≥ 0
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4)...
(In Java) Inheritance Shapes: Write 5 Classes: 1) Shapes    2) Triangle 3) Rectangle    4) Circle 5) TestAllShapes (create 1 object of each type and print the Area for each of them.)
Reflect a triangle whose vertices are ?(2, 3), ?(6, 3) and ?(4, 8) about the line...
Reflect a triangle whose vertices are ?(2, 3), ?(6, 3) and ?(4, 8) about the line ? = 3? + 4. Determine the final coordinates of the triangle. Sketch the initial and final positions
2 Pascal’s Principle An Olympic swimming pool is 50 m long, 25 m wide, and 2...
2 Pascal’s Principle An Olympic swimming pool is 50 m long, 25 m wide, and 2 m deep. (a) Make a plot of pressure as a function of depth in the pool, for 0 ≤ y ≤ 2 m. (b) What is the pressure on the bottom of the pool? (c) What is the total force on the bottom of the pool — the 25 m × 50 m surface? (d) What is the total force on one end of...
Triangle Class write a class for triangle input is the length of the 3 edges you...
Triangle Class write a class for triangle input is the length of the 3 edges you can use arccos: math.acos(you_input_here), the output is -pi to pi (in real value) class math class Triangle(): # initialize here def __init__(xxx): # you need to modify this line          # your function angles is to output the 3 angles (in degree) in ascending order def angles():          # is_equilateral outputs True if the triangle is equilateral def is_equilateral():   ...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
Find the coordinates of the orthocenter of the triangle whose vertices are A(3, 1), B(0, 4) and C(-3, 1).
Find the coordinates of the orthocenter of the triangle whose vertices are A(3, 1), B(0, 4) and C(-3, 1).
Find the centroid of the triangle whose vertices are the points A (8 , 4) B (1 , 3) and C (3 , -1).
Find the centroid of the triangle whose vertices are the points A (8 , 4) B (1 , 3) and C (3 , -1).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT