Skip to content

Commit

Permalink
Update 0119-pascals-triangle-ii.java
Browse files Browse the repository at this point in the history
  • Loading branch information
benmak11 committed Oct 16, 2023
1 parent 372e1c8 commit 06c2640
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions java/0119-pascals-triangle-ii.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,24 @@ public int value(int row, int col, int[][] dp) {
dp[row][col] = value(row - 1, col - 1, dp) + value(row - 1, col, dp);
return dp[row][col];
}

/** Iterative approach to solving the problem - follows Neetcode's solution in Python
* O(n) time complexity
* */
public List<Integer> getRow(int rowIndex) {
List<Integer> res = new ArrayList<>(rowIndex + 1);
for (int i = 0; i < rowIndex + 1; i++) {
res.add(1);
}

for (int i = 2; i < rowIndex + 1; i++) {
for (int j = i - 1; j > 0; j--) {
res.set(j, res.get(j) + res.get(j - 1));
}
}
return res;
}


}
//todo: add bottom up approach

0 comments on commit 06c2640

Please sign in to comment.