From 2e95a888ab6ba0deaf0163af65c5db08dfb08adc Mon Sep 17 00:00:00 2001 From: LeelaTotapally Date: Tue, 12 May 2026 14:20:12 -0400 Subject: [PATCH] "Added solution for pascal" --- Problem1.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Problem1.java b/Problem1.java index e69de29b..d686e8ad 100644 --- a/Problem1.java +++ b/Problem1.java @@ -0,0 +1,27 @@ +Time Complexity: O(n^2) +Space Complexity: O(n^2) +solved on leetcode: yes +any problems faced: no +// Approach: We can generate the pascal triangle by using the previous row. The first and last element of each row is 1 and the rest of the elements are the sum of the two elements above it in the previous row. +class Solution { + public List> generate(int numRows) { + List> pascalTriange = new ArrayList<>(); + List previous = new ArrayList<>(); + previous.add(1); + pascalTriange.add(previous); + for(int i = 1;i temp = new ArrayList<>(); + for(int j=0;j<=i;j++){ + if(j==0 || j==i){ + temp.add(1); + } + else{ + temp.add(previous.get(j-1) + previous.get(j)); + } + } + pascalTriange.add(temp); + previous = temp; + } + return pascalTriange; + } +} \ No newline at end of file