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...
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).
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...
1- Go into a Starbucks and Dunkin - what looks the same and what looks different...
1- Go into a Starbucks and Dunkin - what looks the same and what looks different from a store layout?  How does the Starbucks experience differ than Dunkin. Also - is the product the same in our opinion? 2- Visit a large food store such as ShopRite, Trader Joes, Whole Foods, etc. Look around and see the psycho - design of the shelves, location of food items, products in front and back and shelf space of products. What patterns do you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT