-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpascals-triangle.js
More file actions
61 lines (52 loc) · 1.45 KB
/
pascals-triangle.js
File metadata and controls
61 lines (52 loc) · 1.45 KB
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
// https://leetcode.com/problems/pascals-triangle/
// Related Topics: Array
// Difficulty: Easy
/*
Initial thoughts:
Considering that each row in Pascal's triangle consists of the sum of each adjacent cells
in the previous row with a 1 added to the beginning and end of the row we can
create the triangle with a nested loop.
Time complexity: O(n^2) where n is the number of rows. The outer loop runs n time while the inner
loop runs (1 + 2 + 3 + ... + n-1 + n) times which is n * (n+1) / 2
Space complexity: O(n^2) where n is the number of rows
*/
/**
* @param {number} numRows
* @return {number[][]}
*/
const generate = numRows => {
if (numRows === 0) return [];
const result = [[1]];
for (let i = 0; i < numRows - 1; i++) {
const tempRow = [1];
for (let j = 1; j < result[i].length; j++) {
tempRow.push(result[i][j - 1] + result[i][j]);
}
tempRow.push(1);
result.push(tempRow);
}
return result;
};
/*
Recursive approach
*/
/**
* @param {number} numRows
* @return {number[][]}
*/
const generate = numRows => {
if (numRows === 0) return [];
const result = [[1]];
generateNextRow(numRows - 1);
return result;
function generateNextRow(numRows) {
if (numRows === 0) return;
const row = [1];
for (let i = 1; i < result[result.length - 1].length; i++) {
row.push(result[result.length - 1][i - 1] + result[result.length - 1][i]);
}
row.push(1);
result.push(row);
generateNextRow(numRows - 1);
}
};