118. Pascal's Triangle

#cc #string


Intuition

Approach

Complexity

Space Complexity
Time Complexity

Code

public List<List<Integer>> generate(int numRows) {
	List<List<Integer>> fin = new ArrayList<>();

	if(numRows >= 1) fin.add(Arrays.asList(1));
	if(numRows >= 2)  fin.add(Arrays.asList(1,1));

	List<Integer> prev = Arrays.asList(1,1);
	for(int i = 3; i <= numRows; ++i ) {
		List<Integer> curr = new ArrayList<>();
		curr.add(1);

		for(int j =0; j +1 < prev.size(); ++j ) {
		   curr.add(prev.get(j) + prev.get(j+1)); 
		}

		curr.add(1);

		prev = curr;

		fin.add(curr);

	}

	return fin;
}

Last updated