-
Notifications
You must be signed in to change notification settings - Fork 0
/
pascals_triangle.rs
67 lines (57 loc) · 1.4 KB
/
pascals_triangle.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#![allow(dead_code)]
pub fn generate(num_rows: i32) -> Vec<Vec<i32>> {
let mut result: Vec<Vec<i32>> = Vec::new();
for i in 0..num_rows as usize {
let mut row = Vec::new();
for j in 0..=i {
if j == 0 || j == i {
row.push(1);
} else {
row.push(result[i - 1][j - 1] + result[i - 1][j]);
}
}
result.push(row);
}
result
}
/*
Algorithm - Dynamic Programming - 118. Pascal's Triangle
1. Create a vector of vector of i32
2. Iterate from 0 to num_rows
3. Create a vector of i32
4. Iterate from 0 to i
5. If j == 0 or j == i, push 1 to row
6. Else push result[i - 1][j - 1] + result[i - 1][j] to row
7. Push row to result
8. Return result
Complexity
Time - O(n^2)
Space - O(n^2)
*/
pub fn get_row(row_index: i32) -> Vec<i32> {
let mut result = generate(row_index + 1);
result.pop().unwrap()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_generate() {
let result = generate(5);
assert_eq!(
result,
vec![
vec![1],
vec![1, 1],
vec![1, 2, 1],
vec![1, 3, 3, 1],
vec![1, 4, 6, 4, 1]
]
);
}
#[test]
fn test_get_row() {
let result = get_row(3);
assert_eq!(result, vec![1, 3, 3, 1]);
}
}