-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpascals-triangle-ii.js
More file actions
35 lines (31 loc) · 1.05 KB
/
Copy pathpascals-triangle-ii.js
File metadata and controls
35 lines (31 loc) · 1.05 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
// https://leetcode.com/problems/pascals-triangle-ii/
// Related Topics: Array
// Difficulty: Easy
/*
Initial thoughts:
The naive solution is to handle this problem like with did Pascal's Triangle where we create a two dimensional array
with the values of Pascal's triangle up to the row that we need to return and then return the last row.
Optimization:
Since we only need the last row, we can have a one dimensional array that initially holds the first row of
Pascal's triangle and build uppon it by changing the values in place. This way we will decrease the space complexity
by a factor of n.
Time complexity: O(n^2) where n === index of the required row
Space complexity: O(n) where n === index of the required row
*/
/**
* @param {number} rowIndex
* @return {number[]}
*/
const getRow = rowIndex => {
const result = [1];
for (let i = 1; i <= rowIndex; i++) {
let prev = result[0];
for (let j = 1; j < result.length; j++) {
const curr = result[j];
result[j] = prev + curr;
prev = curr;
}
result.push(1);
}
return result;
};