From ba031b6a0adc0ac540b1ac6ae39fda0172f3949f Mon Sep 17 00:00:00 2001 From: Ayushmaanagarwal1121 Date: Sat, 8 Jun 2024 22:41:28 +0530 Subject: [PATCH 1/2] Create 0119-pascal's-triangle-2.md --- .../0100-0199/0119-pascal's-triangle-2.md | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md diff --git a/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md b/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md new file mode 100644 index 000000000..f2760cec4 --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md @@ -0,0 +1,116 @@ +--- +id: 119-pascals-triangle-ii +title: Pascal's Triangle II +sidebar_label: 0119-Pascal's Triangle II +tags: + - C++ + - Java + - Python +description: "Return the kth row of Pascal's triangle." +--- + +## Problem Description + +Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle. + +In Pascal's triangle, each number is the sum of the two numbers directly above it. + +### Examples + +**Example 1:** + +``` +Input: rowIndex = 3 +Output: [1,3,3,1] +``` + +**Example 2:** + +``` +Input: rowIndex = 0 +Output: 1 +``` + +### Constraints + +- $ 0 <= rowIndex <= 33$ + +--- + +## Solution for Balanced Binary Tree Problem + + + + +### Approach + +To generate the kth row of Pascal's Triangle, we can use a single list to iteratively build up the row. By updating the list from the end to the beginning, we ensure that we are always using values from the previous row (or the initial state). + + +#### Code in Different Languages + + + + + ```java + class Solution { + public List getRow(int rowIndex) { + List row = new ArrayList<>(rowIndex + 1); + for (int i = 0; i <= rowIndex; i++) { + row.add(0); + } + row.set(0, 1); + + for (int i = 1; i <= rowIndex; i++) { + for (int j = i; j > 0; j--) { + row.set(j, row.get(j) + row.get(j - 1)); + } + } + + return row; + } +} + ``` + + + + + ```python + class Solution: + def getRow(self, rowIndex: int) -> List[int]: + row = [1] * (rowIndex + 1) + for i in range(2, rowIndex + 1): + for j in range(i - 1, 0, -1): + row[j] += row[j - 1] + return row + ``` + + + + + ```cpp + class Solution { +public: + vector getRow(int rowIndex) { + vector row(rowIndex + 1, 1); + for (int i = 2; i <= rowIndex; i++) { + for (int j = i - 1; j > 0; j--) { + row[j] += row[j - 1]; + } + } + return row; + } +}; + ``` + + + + +#### Complexity Analysis + +- **Time Complexity**: $O(k^2)$, where k is the given rowIndex. This is because we have nested loops, one iterating up to rowIndex and the other iterating backward up to i. +- **Space Complexity**: $O(k)$, where k is the given rowIndex. This is the space required to store the rowIndexth row. + + + +--- From 42f11de996b8f53e84cc4e62e1731fb9b417b8c7 Mon Sep 17 00:00:00 2001 From: Ayushmaan Agarwal <118350936+Ayushmaanagarwal1211@users.noreply.github.com> Date: Sun, 9 Jun 2024 15:11:00 +0530 Subject: [PATCH 2/2] Update 0119-pascal's-triangle-2.md --- .../lc-solutions/0100-0199/0119-pascal's-triangle-2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md b/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md index f2760cec4..3135b9247 100644 --- a/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md +++ b/dsa-solutions/lc-solutions/0100-0199/0119-pascal's-triangle-2.md @@ -33,7 +33,7 @@ Output: 1 ### Constraints -- $ 0 <= rowIndex <= 33$ +- $0 <= rowIndex <= 33$ ---