Skip to content

Commit 869a0fe

Browse files
Merge pull request #3076 from benmak11/119
Update 0119-pascals-triangle-ii.java
2 parents 372e1c8 + 06c2640 commit 869a0fe

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: java/0119-pascals-triangle-ii.java

+19
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,24 @@ public int value(int row, int col, int[][] dp) {
1717
dp[row][col] = value(row - 1, col - 1, dp) + value(row - 1, col, dp);
1818
return dp[row][col];
1919
}
20+
21+
/** Iterative approach to solving the problem - follows Neetcode's solution in Python
22+
* O(n) time complexity
23+
* */
24+
public List<Integer> getRow(int rowIndex) {
25+
List<Integer> res = new ArrayList<>(rowIndex + 1);
26+
for (int i = 0; i < rowIndex + 1; i++) {
27+
res.add(1);
28+
}
29+
30+
for (int i = 2; i < rowIndex + 1; i++) {
31+
for (int j = i - 1; j > 0; j--) {
32+
res.set(j, res.get(j) + res.get(j - 1));
33+
}
34+
}
35+
return res;
36+
}
37+
38+
2039
}
2140
//todo: add bottom up approach

0 commit comments

Comments
 (0)