Skip to content

Commit f22bc55

Browse files
committed
'Pascal's Triangle II' soln
1 parent a71a1e0 commit f22bc55

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Note: Some solutions have multiple approaches implemented
125125
| x117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) | [Solution](./leetcode/populating_next_right_pointers_ii.rs) | Medium |
126126
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle/) | [Solution](./leetcode/pascals_triangle.rs) | Easy |
127127
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/) | [Solution](./leetcode/pascals_triangle_ii.rs) | Easy |
128-
| 120 | [Triangle](https://leetcode.com/problems/triangle/) | [Solution](./leetcode/triangle.rs) | Medium |
128+
| >120 | [Triangle](https://leetcode.com/problems/triangle/) | [Solution](./leetcode/triangle.rs) | Medium |
129129
| 121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/) | [Solution](./leetcode/best_time_to_buy_and_sell_stock.rs) | Easy |
130130
| 122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/) | [Solution](./leetcode/best_time_to_buy_and_sell_stock_ii.rs) | Medium |
131131
| 123 | [Best Time to Buy and Sell Stock III](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/) | [Solution](./leetcode/best_time_to_buy_and_sell_stock_iii.rs) | Hard |

leetcode/pascals_triangle_ii.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
///
2+
/// Problem: Pascal's Triangle II
3+
///
4+
/// Given an integer rowIndex, return the rowIndex^th (0-indexed) row of Pascal's triangle.
5+
///
6+
/// In Pascal's triangle, each number is the sum of the two numbers directly above it.
7+
///
8+
/// Example 1:
9+
/// Input: rowIndex = 3
10+
/// Output: [1,3,3,1]
11+
///
12+
/// Example 2:
13+
/// Input: rowIndex = 0
14+
/// Output: [1]
15+
///
16+
/// Example 3:
17+
/// Input: rowIndex = 1
18+
/// Output: [1,1]
19+
///
20+
/// Constraints:
21+
/// 0 <= rowIndex <= 33
22+
///
23+
/// Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?
24+
///
25+
26+
// # Solution:
27+
// Time complexity: O(rowIndex²)
28+
// Space complexity: O(rowIndex)
29+
30+
impl Solution {
31+
pub fn get_row(row_index: i32) -> Vec<i32> {
32+
let row_index = row_index as usize;
33+
let mut row = vec![1];
34+
35+
for i in 0..row_index {
36+
let mut next_row = vec![1];
37+
38+
39+
for j in 0..row.len() - 1 {
40+
next_row.push(row[j] + row[j + 1]);
41+
}
42+
43+
next_row.push(1);
44+
45+
row = next_row;
46+
}
47+
48+
row
49+
}
50+
}

0 commit comments

Comments
 (0)